Python3: Data Structures: Dictionaries & Some Interview Questions
We wrap up one of the last data structures in Python, and what it is meant for. Interviewers like to ask 'which data structure is appropriate here?', so a simple cheat sheet is prepared as well.
Table of Contents:
Dictionaries
Creating Dictionaries
Common Dictionary Functions
Iterating Over Dictionaries
Valid Dictionary Key Types
Nested Dictionaries
Interview Questions
1 - Dictionaries
In the real normal world, a dictionary (assuming you still live in the 1900s, and don’t know how to use google) is a book that has definitions of a bunch of words. Every single entry inside a dictionary has 2 parts: a specific word, and it’s definition.
Python dictionaries are like lists & tuples in the sense that you can use them to store a collection of data. However, the main difference is that instead of storing data in a sequence, dictionaries hold information in pairs of data called key-value pairs. In other words, every entry inside a diction has 2 parts: a key, and a value associated with it.
Think about this as a real life dictionary, where the word you are after is the key, and the definition of the word is it’s value. Dictionaries are also more like lists than tuples, because they are mutable just like lists.
2 - Creating Dictionaries
Option 1: use the { } brackets
We can create a dictionary using the { } brackets. Here is an example of a dictionary with the capitals of a lot of American States.
Option 2: use the dict() function
We can use the dict() function in order to convert a nested tuple to a dictionary, or a nested list to a dictionary.
3 - Common Dictionary Functions
Accessing Dictionary Values
If we provide a key to a dictionary, we can then access the value associated with it.
Note: Just a heads up, the above code in line 6 is what we use when we are working with the pandas library in order extract the values associated with that entire column. We will go more in depth on some data wrangling soon.
Adding New Key/Values in a Dictionary
In order to add a new key/value to a dictionary, we have to specify the key we are adding, and the value associated with it.
Remove a Key/Value in a Dictionary
We can use the del keyword, and then specify the key we want to delete. Doing so will automatically delete the key, and the value associated with it.
In the above code, line 6 creates the 'abc' key, and then line 8 deletes it. Hence why we have 'xyz' once in the shell, but then the KeyError.
Checking if a Key Exists
One of the ways we can improve the performance of production code is to check if a certain key exists, before we run the loop over all of the keys in the dictionary. You can check if a key exists in a dictionary by using the in keyword, similar to tuples.
4 - Iterating Over Dictionaries
If we have an extremely large dictionary, sometimes it’s beneficial to just do a quick skim over the keys, and their values in the dictionary we have. Luckily, we can use a for loop to iterate over a dictionary.
You can use the break keyword, alongside an if statement to kill the for loop after a certain number of iterations have been done.
5 - Valid Dictionary Key Types
You can use basically everything except lists as keys in a dictionary. Here is a table you can look at which lists all the possible key types.
6 - Nested Dictionaries
Similariy with lists, we can create nested dictionaries. If you are going to mess around with them, be very careful with the colon (:), brackets ({ }), and the commas (,). Once we’ve created a nested dictionary, we can use the square bracket [ ] twice in order to first point to the sub dictionary, and then the key associated with it.
7- Interview Questions
One of the things Interviewers like to do is give you a scenario, and then ask you which data structure would be appropriate for solving the task at hand. You can basically just use the chunk below as a quick cheat sheet. Dictionaries (especially when not needed) are very expensive in terms of computation power.
Tuples
You do care about the ORDER of the data
You will NOT UPDATE or alter the data during the program running
The primary purpose of the data structure is ITERATION (looping)
Examples: names of months in a year,
Lists
The data has a natural order
You will need to UPDATE or alter the data during the program running
You do care about the ORDER of the data
The primary purpose of the data structure is ITERATION (looping)
Examples: name of your pets, list of phone numbers
Dictionaries
The data is completely UNORDERED (not sorted)
You WILL need to UPDATE or alter the data during the program running
The primary purpose will be to look up values for certain keys
You will need to do some sort of MAPPING
Examples: phone book (has name and associated phone number),
Click here to continue to Python Data Structure Classes.
Great refresher, thank you