Python — Basics of Python Dictionary: Looping & Sorting


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]
# Out: 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'
# 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

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

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'}
# out: 'value1'

# 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'}


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… 😉

64 thoughts on “Python — Basics of Python Dictionary: Looping & Sorting

  1. 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!

    1. i am have also a dictionary as example:
      def meaning(word):
      mydict = {‘apple’:’saib’,’banana’:’kela’,’orange’:’malta’}
      return mydict[word]

  2. also if you want an iterator for keys you can use in a for statement:


    or if you want an iterator for values:


  3. 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,-

  4. 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.

  5. 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:
    chicken salad_______.15$

    without underscores and dots ofcourse,
    i have shopinvent = {
    ‘orange’ : ‘5’
    ‘apple’ : ’42’
    ‘banana’ : ‘230’
    and so on…

  6. 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 : )

    1. hi,i also have an assignment about files and nested dictionaries i dunno if you could help me .i’ld be very glad.thanks

  7. 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?

    1. 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))

  8. ahh i see.. so in therory the .item is a function from the dict class?
    annywho, thanks a lot for all the help : )

  9. 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’, …….}

    1. 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

  10. 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).

  11. 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

  12. 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:
    Any tip for this? 🙂

    (the order is just an example since Dictionaries doesnt have a predefined order to be printed)

    1. 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.

      def recursive_print(value):
          if hasattr(value, '__iter__'):
              for item in value:
          print value
      for value in my_dict.values():

  13. 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 🙂


    1. 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:

      name = {x[name]}
      gender = {x[gender]}
      age = {x[age]}

      Can you give a more practical use case for this specific ordering where you’d want a dictionary to automatically handle that order?

      1. 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:




        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

  14. Aha, the xml was formatted .. lets try this :

    1. 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.

      1. 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?

    2. 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__

      1. 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.

  15. 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

  16. 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?

  17. where is my programming wrong, it keeps on telling me of syntax errors
    if dictionary(vote)>1:
    dictionary(vote) =+1

    dictionary = 1

    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 ()

  18. 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.


  19. HI all
    I want to display ‘#’ symbol in my output display from python script . How can i display this ‘#’- symbol in my output display

    ###Senthilkumar R ###

  20. you can make a dict from pair of values like this:
    mydict = {key:vale for (key, value) in yourlist}

    much simpler.

  21. 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.

  22. 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?

  23. 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.

  24. 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?

  25. 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 .

    Figuring out what you want to do once you finish college can be
    a big part of the battle.

  26. 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 ★★★

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s