Python Forum

Full Version: Dictionary my best option?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am still a beginner and would like to know the best way to approach my task -- whether to structure my data table as a list or dictionary or something else?

The best way to describe my data table is to picture an Excel table with 50 rows and 47 columns.

Column 1 consists of 50 unique two-letter codes (this would by my "key" in a dictionary).  Columns 2 through 48 have variables/numbers (one per column) associated/linked with the unique two-letter code in column 1.

As a beginner, I have used a dictionary to ".get" one variable associated with a unique key.

However, what if I need to .get 47 variables based on the two-letter unique key?  Is that possible with a dictionary?  Is it easy to grab/identify each variable with some kind of loop or something? 

I know this may not be enough information, but it is the best way for me to describe my task
thanks for looking
Quote:As a beginner, I have used a dictionary to ".get" one variable associated with a unique key.

However, what if I need to .get 47 variables based on the two-letter unique key?  Is that possible with a dictionary?  Is it easy to grab/identify each variable with some kind of loop or something? 
yes a for loop. 

You can also just return a list of keys via MY_DICTIONARY.keys() and a list of values with MY_DICTIONARY.values()

If you need to specify the key and not get all you can just loop them and insert an if condition to get the key/value pairs you want

for k,v in MY_DICTIONARY.items():
    #insert if condition here to check key for more filtering
    #k is the key
    #v is the value of that key
Post your code for more explicit help
thanks for the reply -- I need to learn a little more about dictionaries. Happy New Year
Hello!
You can feed a dictionary key with every type of data, objects. You can have nested structures of data. 
Or this:

In [1]: def foo():
   ...:     return 'foo'
   ...: 

In [2]: def bar():
   ...:     return 'bar'
   ...: 

In [3]: d = {'bob': [foo, bar]}

In [4]: d['bob'][0]() == d['bob'][1]()
Out[4]: False
dictionary keys are limited to hashable objects.
I didn't mean the key itself
yeah, the values can be anything, but quite a lot of stuff can be keys.
To be clear to the beginners, dictionary keys can be immutable objects like ints, floats, strings, and tuples. They can't be mutable objects like lists, dictionaries, or sets.
for fun trivia: i made a little dictionary with keys of many different functions (references).