Home » Tutorials » Python » Dictionaries

Dictionaries

Python’s dictionaries are not very common in other programming languages. At least at first, they don’t appear normal. Some super variables like lists, arrays, etc, implicitly have an index tied to each element in them. Python’s dictionary has keys, which are like these indexes, but are somewhat different (I’ll highlight this in just a second). Partnered with these keys are the actual values or elements of the dictionary. Enough with my terrible explanation, let’s go with an example.

Example
myExample = {'someItem': 2, 'otherItem': 20}
print(myExample['otherItem'])
Result 20

See, it’s a little weird isn’t it? If you tried to be an overachiever, you might have tried something like print(myExample[1]). Python will bite you for this. Dictionaries aren’t exactly based on an index. When I show you how to edit the dictionary, you will start to see that there is no particular order in dictionaries. You could add a key: value and, it will appear in random places.

A big caution here is that you cannot create different values with the same key. Python will just overwrite the value of the duplicate keys. With all of the warnings aside, let’s add some more key:values to our myExample dictionary.

Example
myExample = {'someItem': 2, 'otherItem': 20}
myExample['newItem'] = 400
for a in myExample:
    print (a)
Result newItem
otherItem
someItem

Isn’t that crazy how dictionaries are unordered? Now, you might not think they are unordered because my example returns alphabetical order. Well, try playing around with the key names and see if it follows your alphabetical pattern. It won’t. Anyways, adding a key:value is really easy. Simply put the key in the brackets and set it equal to the value. One last important thing about dictionaries.

Example
myExample = {'someItem': 2, 'otherItem': 20,'newItem':400}
for a in myExample:
    print (a, myExample[a])
Result (‘newItem’, 400)
(‘otherItem’, 20)
(‘someItem’, 2)

All we did here was to spit out our whole dictionary. In lists when we told Python to print our variable out (in our case, it’s a), it would print out the value. However, with dictionaries, it will only print out the key. To get the value, you must use the key in brackets following the dictionary’s name. Dictionaries are a little confusing, but they are well worth your patience. They are lightning fast and very useful after you start using them.



Link/cite this page

If you use any of the content on this page in your own work, please use the code below to cite this page as the source of the content.

  • Stewart, Suzy. "Dictionaries". After Hours Programming. Accessed on March 16, 2024. https://www.afterhoursprogramming.com/tutorial/python/dictionaries/.

  • Stewart, Suzy. "Dictionaries". After Hours Programming, https://www.afterhoursprogramming.com/tutorial/python/dictionaries/. Accessed 16 March, 2024.

  • Stewart, Suzy. Dictionaries. After Hours Programming. Retrieved from https://www.afterhoursprogramming.com/tutorial/python/dictionaries/.



21 thoughts on “Dictionaries”

  1. Why was my result:
    otherItem 20
    newItem 400
    someItem 2
    And not:
    (‘newItem’, 400)
    (‘otherItem’, 20)
    (‘someItem’, 2)
    This is my coding:

    >>>myExample = {‘someItem’: 2, ‘otherItem’: 20,’newItem’:400}
    >>> for a in myExample:
    print (a, myExample[a])
    I use v. 3.3

  2. I’m running Python 3.7.0 and this is what I got…
    myExample = {‘someItem’: 2, ‘otherItem’: 20, ‘newItem’: 400}
    for a in myExample:
    print (a, myExample[a])

    someItem 2
    otherItem 20
    newItem 400
    >>>

  3. Hi man following your tuturial its great but to be honest the beginning that is this text:
    “Python’s dictionaries are not very common in other programming languages. At least at first, they don’t appear normal. Some super variables like lists, arrays, etc, implicitly have an index tied to each element in them. Python’s dictionary has keys, which are like these indexes, but are somewhat different (I’ll highlight this in just a second). Partnered with these keys are the actual values or elements of the dictionary. Enough with my terrible explanation, let’s go with an example.

    is terrible 😀 I had to read it for like 5 times to get it 😀 Pls for next newbie like me rewrite it 🙂
    thx for doing this

  4. Just a comment ¬¬¬
    Dictionaries are very common in all other languages, they differ from indexed arrays to become named arrays, this is, they are key:value pairs forming and array of keyed values not of indexed values. Some languages use both at same time indexed and named (or keyed), but not all. This named arrays are so powerfull that are the base of javascript which treats them and creates them as objects since the key may refer to any kind of value, including functions, procedures, and to other objects including other named arrays, in this case dictionaries. I would have call tehm obray (arrays of objects) not dictionaries

  5. Try this code to get an idea of sorting
    myExample = {‘AAA’: “Bear”, ‘BBB’: 20,’CCC’:400}
    for a in myExample:
    print (a, myExample[a])

    Output:
    (‘AAA’, ‘Bear’)
    (‘BBB’, 20)
    (‘CCC’, 400)

    It seems as though python sorts by key name not value by default. Also lowercase and uppercase matters as well:
    (‘BBB’, 20)
    (‘CCC’, 400)
    (‘aaa’, ‘Bear’)
    or

    (‘BBB’, 20)
    (‘aaa’, ‘Bear’)
    (‘ccc’, 400)

  6. If you get output that doesn’t have the parenthesis and single quotes, it is because you are using python 3. I have both 2.7 and 3.4 installed, and 2.7 gives the results shown (but earlier stuff in the tutorial only works with 3, so not quite sure what’s going on).

  7. I just realized that a dictionary could be usefull when I’m making a program to help train on my homework before tests. The index/key could be the question/word and the answer could be the value

  8. Great, it’s very important to know how to deal with Dictionaries in Python. But the order of the result in the Python 3.3.5 console is not the same as here.

  9. could someone explain why the numbers aren’t listed in the first result, but in the last result? This is fairly vague but if you check above, i think you’ll understand what I mean.

  10. Look at this example:

    def pippo(a):
    print (str(a)+" works")
    esempio={‘oggetto1′:pippo("python"),’oggetto3′:10,’oggetto2’:25}
    esempio[‘oggetto4’]="3000 pounds"
    #esempio={‘oggetto5′:50,’oggetto6’:70}
    for b in esempio:
    print(b,esempio[b])

    The result is:

    python works
    (‘oggetto3’, 10)
    (‘oggetto2’, 25)
    (‘oggetto4’, ‘3000 pounds’)

    Why the first object has miss its dictionary name ?

  11. I got these results in Python 3.3.2
    >>> myExample = {‘someItem’: 2, ‘otherItem’: 20, ‘newItem’:400}
    >>> for a in myExample:
    print (a, myExample[a])

    newItem 400
    someItem 2
    otherItem 20
    >>>

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.