By Tristan Martello, Co-Founder at Kickstart STEM
Graduates of our Kickstart Python class (or coders familiar with Python) may recall the importance of lists and tuples when making programs. These structures are crucial to Python, and programming in general; the ability to store multiple variables as ordered items in a list is incredibly powerful. Dictionaries are another type of variable used to organize data in a program. They are similar to lists, but also have some very important differences.
The first, and most obvious difference is the notation. Lists in Python are declared with square brackets, while dictionaries are declared with curly braces. In the code segment below, line one is the declaration of an empty list, and line two is the declaration of an empty dictionary.
The second, and most important difference between lists and dictionaries is their structures. Lists are known as an ‘Ordered Variable’: this is fairly straightforward, their contents are ‘in order’, organized by index. The first element in a list is always found at index zero, the second element is always at index one, and so on. If you have a list and want to find the third element, all you have to do is ask for the element at index 2:
Code:
Result:
Since the elements are all stored in order, running this code segment will print the element at index 2 in the list, which is the string ‘Blueberry’.
Dictionaries, however, are known as an ‘Unordered Variable’. This means that unlike lists, the elements are not stored in order, and you cannot use any commands relating to index. Instead, dictionaries are organized with ‘Key value pairings’. Since these structures are called dictionaries, you can think of keys and values as words and definitions. Each key in a dictionary has a value associated with it, just like each word in a dictionary has a corresponding definition. The following code defines a dictionary variable that behaves like a real life dictionary:
This dictionary definition contains three key-value pairings. The keys are the strings “Apple”, “Peach”, and “Blueberry”, and the values are descriptions of each fruit. In this type of dictionary definition, the key and value are separated by a colon, and there is a comma between each key-value pair.
In this example, all the keys and values are strings, but you can mix and match variable types however you want; strings, floats, ints, and even lists are acceptable options for both keys and values.
The previous dictionary definition shows how to define a whole dictionary at once. But what if you want to add items to the dictionary after you initially create it? Or what if you want to change the value for a certain key? Just like how you change the element at a certain index of a list, you change the value for a certain key in a dictionary by using brackets.
On line 3, this code segment creates a new key called “Coconut”, and pairs it with the value “A round, shelled, hairy, fruit”. On line 4, this code segment takes the key “Apple” and pairs it with a new value, “Big red fruit grown in the fall”.
Since dictionaries are unordered, you can’t use indexes to find items. If I typed in myDictionary[0] to try and get the description of an apple, the program would crash. The notation for finding items in the dictionary is similar to the definitions in lines 3 and 4 of the code above; you use keys inside brackets to find values that already exist.
Code:
Result:
Line 6 of the code segment above will print out “A round, shelled, hairy fruit”, since that is the value associated with the key “Coconut”.
The last important aspect of dictionaries is how they work with loops. Since dictionaries are unordered, loop structures that correspond to indexes (while loops and for loops) are not intuitively compatible with dictionaries. Foreach loops, however, work wonderfully with the key-value pairings of a dictionary. The following code shows how to use a for loop to iterate through all the items in a dictionary.
Code:
Result:
There are two important takeaways from the foreach loop in that code segment. First, in line 6 (the loop declaration), the foreach loop iterates through all the keys in myDictionary- this means that the loop runs one cycle for each key. In this case it runs four times, for Apple, Peach, Blueberry, and Coconut. This also means that each time the loop runs, the variable fruit is declared as a different string- those four fruits.
The second important takeaway is in line 8 where we use the same accessing notation as before. Except this time, instead of having a specific string inside the brackets, we have the variable fruit. Since fruit is declared as a different string each time the loop runs, line 8 prints a different definition each time. This structure is why we can use two lines of code (lines 7 and 8) to print 4 different key-value pairings.
If you can recreate the code above and explain how it works, you have mastered the basics of python dictionaries! Remember, the examples shown here only demonstrate a small fraction of what you can do with dictionaries, I’d recommend practicing with these on your own to explore different applications and variable type combinations. Good luck!
Tristan, you’re my favorite person ever. Keep doing what you’re doing