Here some bits of info about python dictionaries & looping through them.
Extra special beginner stuff.
What is a dictionary?
A python dictionary is an extremely useful data storage construct for storing and retreiving key:value pairs.
Many languages implement simple arrays (lists in python) that are keyed by a integer. For example, if you made a list [1, 2] – the first value would be retrieved via [1, 2][0].
my_list = [1, 2, 3] my_list[0] # Out: 1 my_list[1] # Out: 2
A dictionary is a little more advanced in that the keys can be many other things than integers. Often times the key will be a string merely for the fact that it’s easy for a human to recall.
Will I remember that my_list[3] is my phone number? Not nearly as well as if I have a key named “phone_number”.
my_dict = { 'key1': 'value1', 'key2': 'value2', 'key3': 'value3' } my_dict['key1'] # Out: 'value1'
Major differences vs lists
– Keys are any hashable object (say strings for simplicity)
– Are NOT ordered (a list is by definition ordered)
One way I like to think about them are as little variable containers. The fact that they are wrapped in a container makes them quite useful and versatile since you can easily move the “container” around.
In fact, variables are very much related to dictionaries! Whenever you declare a variable (x=3), that variable is accessible by its string name (‘x’) via the dictionary returned by the builtin function ‘locals()’ or ‘vars()’.
Watch and see:
var1 = 'val1' # local variable definition var2 = 'val2' locals_dict = locals() print locals_dict['var1'] # Out: val1 container = {} container['var1'] = 'val1' print container['var1'] # Out: val1
Looping through dictionaries
Now, if you did a little experimenting, you would see that if you loop through a dictionary you loop through its keys.
>>> my_dict = { ... 'key1': 'value1', ... 'key2': 'value2', ... 'key3': 'value3' ... } >>> for item in my_dict: ... print item ... key3 key2 key1
Note that this is the equivalent of looping through “my_dict.keys()”
What about getting the values?
Based on what we’ve learned, you could always use the keys you are iterating through to pull the value from the dictionary.
for item in my_dict: print my_dict[item]
But there are better ways to get the values. Enter the “items” function.
We can ask the dictionary to return key, value pairs via the “items()” function.
for key, value in my_dict.items(): # returns the dictionary as a list of value pairs -- a tuple. print key, value
More efficient dictionary loops
Calling “my_dict.items()” requires generating the entire list of key-value pairs in-memory. Sometimes, if your dictionary is too large, this can be a severe performance bottleneck. To get around this problem we can create a generator via the “iteritems()” method.
A generator allows you to iterate one item at a time. Only the key and value are pulled into memory for every iteration and immediately discarded. There are methods for returning a key generator and value generator as well.
for key, value in my_dict.iteritems(): print key, value for key in my_dict.iterkeys(): print key for value in my_dict.itervalues(): print value
Note that one thing you can’t do with a generator is to delete a key during the generator loop.
for x, y in mydictionary: del mydictionary[x] # Out: RuntimeError: dictionary changed size during iteration
Sorting a Python Dictionary
Actually, python dictionaries can’t be sorted. We will have to turn them into lists to sort them.
# to my dictionary... sorted_list = [x for x in my_dictionary.iteritems()] sorted_list.sort(key=lambda x: x[0]) # sort by key sorted_list.sort(key=lambda x: x[1]) # sort by value # to reverse the sort sorted_list.reverse()
Useful dictionary tips
Accessing a dictionary key that might not exist
Sometimes we know that a dictionary key might not exist. This happens a lot in loops where we don’t want to use a try/except block just to capture the exception.
For these situations we have the “get” method. Pass in the key as the first argument, and it will either return the value or None.
Pass in a second argument, and if the key doesn’t exist, it will return the second argument.
dict_ = {'key1':'value1'} dict_.get('key1') # out: 'value1' dict_.get('key2') # out: None dict_.get('key2', "Key doesn't exist") # out: Key doesn't exist
Get or insert into a dictionary if key doesn’t exist
Sometimes we need to insert a value if the key doesn’t exist in a dictionary. The previous “get” method only returns a value – the dictionary is unchanged.
dict_ = {} dict_.setdefault('key1', 'value1') # Out: value1 print dict_ # Out: { 'key1': 'value1' }
This can be extremely useful if you need to append to a list if it exists or otherwise create a blank list.
key_value_pairs = [ ('key1', 'value'), ('key1', 'value2'), ('key1', 'value3'), ] dict_ = {} for key, value in key_value_pairs: dict_.setdefault(key, []).append(value) print dict_ # Out: { 'key1': ['value','value2','value3'] }
Generate a dictionary from tuples
It’s often useful to generate a dictionary from a list of tuples. For example, you could use a list comprehension to create a dictionary!
key_value_pairs = [ ('key1', 'value'), ('key2', 'value2'), ('key3', 'value3'), ] # now let's generate the same list of tuples via list comprehension key_value_pairs = [('key{0}'.format(x), 'value{0}'.format(x)) for x in range(1, 4)] dict_ = dict(key_value_pairs) print dict_ # Out: {'key3': 'value3', 'key2': 'value2', 'key1': 'value1'}
Conclusion
I’m a little surprised that this is one of my most visited blog posts.
It’s not written well, and it was written years ago!
Let me know if I can improve it… π
Hi Ken,
There is also this form which is only a little more advanced:
for key, value in myDictionary.iteritems():
This uses iteritems to access a generator of the key,value pairs – so on a large dictionary you don’t waste memory generating a large intermediate list; and uses tuple assignment to assign to key and value.
– Paddy.
P.S. Welcome to Python!
i am have also a dictionary as example:
def meaning(word):
mydict = {‘apple’:’saib’,’banana’:’kela’,’orange’:’malta’}
return mydict[word]
Thankyou lots, I’ve been looking for an article like this everywhere!
also if you want an iterator for keys you can use in a for statement:
myDictionary.iterkeys()
or if you want an iterator for values:
myDictionary.itervalues()
i want know how to iterate no of rows in for statement and i have to find max score of intents
This is probably the tenth time I’ve come to this page when I can’t remember how to loop a dictionary. Thanks for writing this!
NP! Thanks for commenting
thank you! for some reason the python docs seem to hide how to do this from mere mortals
thanks! this is exactly the type of thing i was looking for!
could you also give an example of how to sort in columns?
eks. if i wanted this result after printing from for item in my_dict: print item ?
Object 30,-
Object 30,-
Object 30,-
Object 30,-
Hey Anaconda,
Can you clarify please? I’m not sure I understand.
Your demonstration of how to loop through keys in a dictionary, or values, or both, and how to use a generator are so fundamental I think they need to be included in the standard python documentation.
This cleared up a whole lot. The one area I debate is whether there is a significant speed boost from using a dictionary and looping, or a list of dictionaries? I often use the latter but don’t know how much slower it is.
i see now that the text was posted without spaces…
i wanted to print an inventory organiced in two columns. object and price lined up like this:
orange____________..5$
apple______________42$
banana____________.230$
pie________________10$
chicken salad_______.15$
without underscores and dots ofcourse,
i have shopinvent = {
‘orange’ : ‘5’
‘apple’ : ’42’
‘banana’ : ‘230’
and so on…
for key, value in dict.items():
print(‘{0:<10}{1}'.format(key, value))
tried your code, wich seems nice and effective.. but i cant seem to make it work. I get the following error:
for key, value in shop.shopinvent():
TypeError: ‘dict’ object is not callable
here is a screen with the relevant code (easier then pasting here :))
thanks alot for the help : )
Anaconda, I’m not sure why you would call your dictionary. To iterate over key-value pairs, you need to call items or iteritems().
hi,i also have an assignment about files and nested dictionaries i dunno if you could help me .i’ld be very glad.thanks
im not sure i follow.. i am as you probably figured out pretty new to python. which part of the code are you referring to?
it appears `shopinvent` is a dictionary. You can not call a dictionary as the error suggests. You can’t call — {}() just as you can’t call an integer 100().
You need to call the “iteritems()” or “items()” method on the dictionary to return key/value pairs.
Copy and paste:
for key, value in shop.shopinvent.items():
print(“{0:<10}{1}'.format(key, value))
ahh i see.. so in therory the .item is a function from the dict class?
annywho, thanks a lot for all the help : )
Yes that’s exactly right – items is a method on the python dict class. NP!
hi Yuji,
how do i iterate through a key that is combinational..? i mean, when the key is a set of 3 or 4 different Strings/numbers, then how do i get the data in first two of the set.
eg: dict = { ‘sham, ravi, 098, str123’: ‘ valur1,asdf2,qwer3,…so on’, …….}
Smith, you key is just a string in your example. Your question is more: “how do I convert a string to a list”.
for k, v in dict.items():
key.spilt(‘,’)[0:2] # get first two of string split by comma
very well written….
Thanks, this really helped!
Nice writeup!
A minor correction in one of the examples above:
var1 = ‘val1’ # local variable definition
var2 = ‘val2’
locals_dict = locals()
print locals_dict[‘val1’] <<<— wrong value
print locals_dict['var1'] <<<— should be 'var1'
Another way to show the output would be to use assert:
assert locals_dict['var1'] == 'val1'
Also in your last example "Generate a dictionary from tuples" you mention using list comprehension to create the dictionary. I see the key_values_pairs list but no list comprehension (or maybe I don't comprehend list comprehension).
Thanks! I fixed the locals dict error.
As for the list comprehension, yes, I don’t use one in an example, just merely suggesting that creating dictionaries from tuples can be useful because you can create lists of tuples via list comprehensions! Now we have dict comprehensions, but I shy away from those until I’m fully on a 2.7 environment.
I’ll add a list comprehension in there
What about accessing Values in a Dictionary with “List” and “Normal” entries?
i.e: mydict ={Key1: [value1,value2], Key3=’value6′,Key2=[value3,value4,value5]}
How I would print each value in a different line?
1 for item in my_dict:
2 print my_dict[item]
This code will print:
value1 value2
value 6
value3 value4 value 5
But I need:
value1
value2
value6
value3
value4
value5
instead.
Any tip for this? π
(the order is just an example since Dictionaries doesnt have a predefined order to be printed)
Then you’d need to loop over the iterables.. If you want unlimited nesting support, you’d need a recursive function that calls itself until the value is not iterable.
Hi, thanks for the write up, its very useful to me.
Could you tell me if / how I would sort a dictionary in a bespoke order. For example I have a dictionary like:
{‘dob’: ’03/04/1978′,
‘gender’: ‘Male’,
‘age’: ’34’,
‘address’: ‘Rockley Road’,
‘name’: ‘Sean’}
but I want to print for example in the order:
name = Sean
gender = Male
age = 34
dob = 03/04/1978
address = Rockley Road
I was thinking I could use a list with the order I want and iterate through that at the same time as iterating through my dictionary but all the iterating is making me dizzy π
Thanks
Sean, it looks to me like you want to visually format your output in an arbitrary, human-specific order. In this case you should not be relying on mathematical ordering but format output directly:
Can you give a more practical use case for this specific ordering where you’d want a dictionary to automatically handle that order?
Thanks Yuji,
The reason I cant simply do that is because I don’t know up front what the dictionary will contain.
The dictionary is being created at run time based on the contents of an xml file and a user defined list of elements to collate.
Imagine the following xml file:
Sean
34
Blonde
….
….
….
Now I dont know how many ‘Entry’ elements there will be and I also don’t know which elements will be selected.
In my code I have the selected elements in a list of strings and I generate a new list of dictionaries by walking through the xml file and from every ‘Entry’ element pull out the necessary values in a {node_name: value, node_name: value, ….} dictionary.
I want the output to match the order of the list as these are added to the list by the user at run time.
Maybe I am using the wrong tools for the job?
Thanks again
Aha, the xml was formatted .. lets try this :
[root]
[Entry]
[Name]Sean[/Name]
[Age]34[/Age]
[HairColour]Blonde[/HairColour]
….
[/Entry]
[Entry]
….
[/Entry]
….
[/root]
Ah ha! If the ordering is dictated by the XML you can look into collections.OrderedDict. Note there are small implementations for older python versions.
This dict preserves input order.
Of course! How frustratingly simple (just like me ^_^) .. Thank you, I will go and have a play with the ordered dictionary.
Glad I’m not stuck using Python 2.6 or older .. is this even possible in older versions?
Hey Sean, yes it’s possible in older versions — lots of backwards compatible programs implement their own because it’s quite short.
I think it’s actually a great insight into subclassing basic types in Python. I haven’t done it often, but it’s pretty cool!
I love how you can overload __getitem__ __setitem__
I just googled for what you meant by ‘subclassing basic types in Python’ and I can agree with you on that .. its pretty amazing.
I am still quite new to programming and Python in particular and I am definitely impressed with the flexibility of this language.
Thanks again for your help earlier. Much appreciated.
It is remarkable, rather the helpful information
P.S. Please review our icons for Windows 8
Good to get an idea
how can i merge 2 dictionaries which contain same ‘key value’ without using defaultdict(list) function?
A very informative blog and well laid out. I will be refering people who are new to python
Thanks for the info!
FYI Under “Generate a dictionary from tuples” the comment output of print dict_ should read: {‘key3’: ‘value3’, ‘key2’: ‘value2’, ‘key1’: ‘value1’}
Where: value1 and not value
Dictionaries are unordered, so it’s not really an issue, but I’ve updated it!
This blog is great on dictionary. If you have blogs for other data structures, where could I find them.I have one question regarding converting a list of tuples to dict.
I tried k_v =[(‘key1’, 9), (‘key2’, 10), (‘key3’, 12)]
di = dict(k_v)
but I get the error message as
Traceback (most recent call last):
File “”, line 1, in
TypeError: ‘dict’ object is not callable
what could this be too?
sri, restart your python session. You have renammed built-in `dict` to a dictionary instance.
where is my programming wrong, it keeps on telling me of syntax errors
while(vote!=”DONE”):
if dictionary(vote)>1:
dictionary(vote) =+1
else:
dictionary = 1
The error should tell you what error and what line. Are you sure it’s not suggesting something like “Dict object is not callable”? You access dictionary keys via square brackets [] not parenthesis ()
Your post came up #4 in my search for “for loop on dictionary python”. and helped me understand how to obtain values when iterating a dictionary and taught me things I haven’t seen before as I learn python. Such as “for key, value in dictionary”.
I hadn’t seen a for loop with 2 iterating values before, nor the iter methods/ key generators.
I’ll be back for more.
Thanks
Very nice piece.
HI all
I want to display ‘#’ symbol in my output display from python script . How can i display this ‘#’- symbol in my output display
###Regards###
###Senthilkumar R ###
Not sure what’s stopping you. print(“#FOO”). string = ‘#oo’ etc
Can we make a dictionary with keys only and not the values i.e. with empty keys.
you can make a dict from pair of values like this:
mydict = {key:vale for (key, value) in yourlist}
much simpler.
If you need to sort a Python dictionary often, then check out the sortedcontainers module on Python. It has a fast, pure-Python implementation of a dictionary type that maintains the keys in sorted order.
I have a dictionary of images and i need to plot those images. i’m having trouble in plotting them. ndarray of images are of type uint8, so how to display/plot based on keys?
How would I read an excel spreadsheet, with 3 columns (name, cigar, rating). to create a nest dictionary in Python. For example, I want member={‘name’: {‘cigar’: ‘rating’, ‘cigar’: ‘rating’}, {‘name’: {‘cigar’: ‘rating’}, etc.
Thanks for any and all help.
Reblogged this on Nychos' Blog.
I have a dictionary, but when I loop with iteritems, it feels like they come in random order? Not the way I inserted the (key, values), or any sorted order.
I would like that iteritems, retrieves in the same way items where insreted. Can this be done? And why is iteritems so strange?
This page gets right to the point. Well explained, and good examples. Thanks!
Great tutorial. Thanks for sharing this with us; Jesus Christ Bless! π
Thanks for this!
I found this much more useful than the actual docs lol
We offer four bachelor levels, two grasp’s levels, as well as a minor in mathematics, a Certificate in Introductory Actuarial Mathematics, and an issue Authorization https://math-problem-solver.com/ .
Figuring out what you want to do once you finish college can be
a big part of the battle.
Thx, a great post, as usual! Let me share something new with you and with your audience. I have a profound gift idea for you.
From just Β£9.99 with β FREE β NEXT DAY β delivery for orders Over Β£20
and 100% money back guarantee. Highest quality canvas prints online – get started now!
Did you know that you can print from Facebook & Instagram TODAY and hang them on the wall as canvas prints?
Bring your online memories into real life, and give to somebody you love π with an option to print from your favorite social media, or your smartphone or tablet.
I print my images with Parrot Print Canvas β β β https://www.parrotprintcanvas.com/canvas-prints