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

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

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

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.

If you enjoyed this resource, support me by sharing this page with others.
Share on Facebook
Facebook
Share on Google+
Google+
Share on StumbleUpon
StumbleUpon
Digg this
Digg
Tweet about this on Twitter
Twitter
Share on Reddit
Reddit

Leave a Comment

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

  3. Dictionaries are very similar to Maps in java.An extremely powerful data structure.But the python’s list implementation is so much easier to learn.

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

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

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

  7. This looks like functions in mathematics: each independent variable (key) can only output one outcome.
    Can the value be a tuple or a list?

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

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

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

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

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *

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