Python Forum
Dictionary my best option? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Dictionary my best option? (/thread-1403.html)



Dictionary my best option? - birdieman - Dec-31-2016

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


RE: Dictionary my best option? - metulburr - Dec-31-2016

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


RE: Dictionary my best option? - birdieman - Dec-31-2016

thanks for the reply -- I need to learn a little more about dictionaries. Happy New Year


RE: Dictionary my best option? - wavic - Dec-31-2016

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



RE: Dictionary my best option? - Skaperen - Dec-31-2016

dictionary keys are limited to hashable objects.


RE: Dictionary my best option? - wavic - Dec-31-2016

I didn't mean the key itself


RE: Dictionary my best option? - Skaperen - Dec-31-2016

yeah, the values can be anything, but quite a lot of stuff can be keys.


RE: Dictionary my best option? - ichabod801 - Dec-31-2016

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.


RE: Dictionary my best option? - Skaperen - Jan-01-2017

for fun trivia: i made a little dictionary with keys of many different functions (references).