Skip to Content

Advertisement

You (Level 0)
0% Complete
Last Badge Earned
None

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.

For a more tangible and better look into the Python language, consider reading the following book. It's an excellent read.

Your code will execute in this window.



Let's explore some more tutorials or topics!

Next Tutorial Previous Tutorial

Advertisement

If you enjoyed this resource, support me by sharing this page with others.

Comment on

Show Comments
  • User

    Neon July 9, 2015, 11:56 p.m.

    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 :D I had to read it for like 5 times to get it :D Pls for next newbie like me rewrite it :-) thx for doing this

  • User

    David May 18, 2015, 2:31 a.m.

    Can we remove a key and value from a dictionaries?

  • User

    Foro5 March 28, 2015, 7:09 a.m.

    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

  • User

    BRIAN March 26, 2015, 6:37 a.m.

    Easy stuff!!! :)

  • User

    LordBeeg March 3, 2015, 2:52 p.m.

    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)

  • User

    Ronald Pottol Feb. 1, 2015, 12:04 a.m.

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

  • User

    Deepstream April 24, 2014, 11:34 p.m.

    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

  • User

    Vicki April 20, 2014, 10:31 a.m.

    Dictionaries remind me of Associative Arrays in other languages!!!

  • User

    Andy M April 3, 2014, 9:19 p.m.

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

  • User

    panda March 17, 2014, 6:17 a.m.

    By what orders on earth are dictionarys sorted?

  • User

    Satya March 7, 2014, 7:36 p.m.

    http://www.afterhoursprogramming.com/tutorial/Python/Dictionaries/ Python's dictionaries are not exactly normal in other programming languages may be corrected to read: Python's dictionaries are not exactly normal as they are in other programming languages

  • User

    Lotfi GHAZOUANI March 3, 2014, 8:16 a.m.

    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.

  • User

    cao xuefeng Jan. 9, 2014, 8:18 p.m.

    why the order practice on python 3.3 is difference with there order.

  • User

    Adrian M Dec. 29, 2013, 9:37 p.m.

    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.

  • User

    The Aztek Dec. 9, 2013, 8:33 a.m.

    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 ?

  • User

    Nitish P Nov. 30, 2013, 8:38 a.m.

    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.

  • User

    Rebarakaz Sept. 14, 2013, 4:47 a.m.

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

  • User

    Benjo Ben July 11, 2013, 12:39 p.m.

    Because you do not know in what order are the elements saved in the dictionary.

  • User

    Sofie Larsen Feb. 23, 2013, 8:46 a.m.

    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



Advertisement