Writing a table with values for several years as a dictionary?
<p>I have a table I want to write as a Python code, but I'm stuck and can't figure out how to do it.</p>

<p>I have a table with data for two years (1991 and 1992), and different values for each year (men: 35 (1991) and 42 (1992), women: 38 (1991), 39 (1992) and children: 15 (1991), 10 (1992).</p>

<p>What I want is to be able to write one variable (dictionary) in python that makes me able to search for a specific value in a specific year (ex: men(1992) = 42).</p>

<p>My best suggestion so far is to make a dictionary including tuples in something like this:</p>

<pre><code>people = {
    'year': (1991, 1992),
    'men': (35, 42),
    'women': (38, 39),
    'children': (15, 10)
    }
</code></pre>

<p>But this obviously not make it possible to search for both specific values in a specific year.</p>

<p>I would suggest something like the following:</p>

<pre><code>people = {'1991':{'men':35, 'women':38, 'children':15},
          '1992':{'men':42, 'women':39, 'children':10}}
</code></pre>

<p>Then you can access specific example data using:</p>

<pre><code>print(people['1991']['men'])
</code></pre>

<p><strong>EDIT</strong></p>

<p>If you really need to use tuples and also need the identifiers/keys you will have to use <strong>lists of tuples</strong> like so:</p>

<pre><code>people = {'1991':[('men', 35), ('women', 38), ('children', 15)],
              '1992':[('men', 42), ('women', 39), ('children', 10)]}
</code></pre>

<p>With this variation you can access the same data like:</p>

<pre><code>print(people['1991'][0][1])
</code></pre>

python dictionary tuples