Python Forum

Full Version: Build a multi-variable dictionary
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to build a multi-variable dictionary.  Below is an abbreviated version:
Table = {BA: 5000, 6000, 7000, 8000, 10000, .5, .6, .8, .5, .4}
Ultimately, the Table will have about 40 keys and each one will have 35 variables.

I am using Pycharm, and when I keyed in the above (just getting started), I received an "unresolved reference" on the "BA", and I received an "expected end of statement" at the end of the } on the right.

Why the unresolved reference?  Can a dictionary have 35 keys?  Is the syntax of coding a multi-variable dictionary than having one variable?  

As you may be able to tell, I am very new to Python.

thanks for looking
it looks like you want to create a dict where key is string and value is tuple/list
e.g.

>>> Table = {'BA': (5000, 6000, 7000, 8000, 10000, .5, .6, .8, .5, .4), 
             'DC': (2000, 1000, 500, 250, 125, 0.1, 0.2, 0.3, 0.15, 0.05)}
>>> Table['BA']
(5000, 6000, 7000, 8000, 10000, 0.5, 0.6, 0.8, 0.5, 0.4)
>>> Table['DC'][2]
500
Under your first example Table = {'BA': (5000, 6000, .......) -- I would be able to lookup (using .get, I think) the 'BA', and then retrieve all 35 variables associated with that key. Correct???


thanks for responding
yes, you can access  the value for given key using get. In my post I had given an example how you can access value for given key and also how can access element in that value. Here is the example again
>>> Table = {'BA': (5000, 6000, 7000, 8000, 10000, .5, .6, .8, .5, .4), 
             'DC': (2000, 1000, 500, 250, 125, 0.1, 0.2, 0.3, 0.15, 0.05)}
>>> Table.get('BA')
(5000, 6000, 7000, 8000, 10000, 0.5, 0.6, 0.8, 0.5, 0.4)
>>> Table['BA']
(5000, 6000, 7000, 8000, 10000, 0.5, 0.6, 0.8, 0.5, 0.4)
>>> Table.get('BA')[4]
10000
>>> Table['BA'][4]
10000
or iterate over the elements in the value associated with the key:
>>> for value in Table['BA']:
    print value
Output:
5000 6000 7000 8000 10000 0.5 0.6 0.8 0.5 0.4
thank you