A dictionary is useful when you want to associate a key with a value.
A key is for example the first name of a person and the value his last name.
But this is the same for a car and its color.
Of course, it is possible to have a dictionary of dictionaries of dictionaries of dictionaries.
This is a bit more complex for your brain although it works as well.
In this context, a dictionary is like a database that contains all data necessary.
Let's see first an easy example in this tutorial of how using a dictionary in Python 3.
In this step we are going to retrieve the last name of "sarah" in the dictionary.
We pass this name in the function and we put this name into the square brackets to have our result.
# BEGIN def dicoManager1(name, theDico): print('The last name of "' + name + '" is "' + str(theDico[name]) + '".') # END myDico1 = {'john': 'smith', 'sarah': 'connors', 'darth': 'vader' } name1 = 'sarah' dicoManager1(name1, myDico1)
The last name of "sarah" is "connors".
# BEGIN def dicoManager2(name, theDico): print('The complete name is "' + theDico[name]['first'] + '" "' + str(theDico[name]['last']) + '".') # END myDico2 = {'completeName': {'first': 'sarah', 'last': 'connors'}, 'age': '38', 'city': 'los angeles' } name2 = 'completeName' dicoManager2(name2, myDico2)
The complete name is "sarah connors".
We are displaying the item number of the weapons' item with the line:
while i < len(theDico[name]):
So with a while statement we can easily display these elements.
# BEGIN def dicoManager3(name, theDico): print('The favorite weapon of Sarah is the "' + theDico[name][0] + '".') print('Displaying all weapons:') i = 0 while i < len(theDico[name]): print(theDico[name][i]) i += 1 # END myDico3 = {'completeName': {'first': 'sarah', 'last': 'connors'}, 'age': '38', 'city': 'los angeles', 'weapons': ['shotgun', 'ak47', 'knife'] } name3 = 'weapons' dicoManager3(name3, myDico3)
The favorite weapon of Sarah is the "shotgun". Displaying all weapons: shotgun ak47 knife
# BEGIN def dicoManager(name, theDico): print(name) print(str(theDico) + '\n') # END myDico = {'john': 'smith', 'sarah': 'connors', 'darth': 'vader' } myDico3 = {'room': 'kitchen', 'hifi': 'tv', 'bed': 'king size', 'sarah': 'lol' } myDico2 = myDico.copy() dicoManager('copy() --> copy the dictionary into another one.', myDico2) myDico.clear() dicoManager('clear() --> clear all the dictionary.', myDico) myDico = myDico2 dicoManager('Elements in "myDico".', myDico) myDico2 = myDico2.fromkeys(myDico2, ['hello', 'world']) dicoManager('fromkeys() --> change all values from myDico2 to a list of items.', myDico2) myStr = myDico.get('sarah'); print('get() --> retrieve the value associates with the key "sarah", in our case:\n"' + myStr + '".\n'); myDico2 = myDico.items() dicoManager('items() --> retrieve all pairs (key, value) of the dictionary.', myDico2) myDico4 = myDico.keys() dicoManager('keys() --> retrieve all keys of the dictionary.', myDico4) myDico2 = myDico.pop('sarah') dicoManager('pop() --> remove the key and value corresponding at the key passed in parameter and return the value.', myDico2) print('Elements present in myDico after pop():') print(str(myDico) + '\n') myDico.popitem() dicoManager('popitem() --> remove the first pair (key, value) of the dictionary.', myDico) myDico = myDico3.copy() myDico.setdefault('film', 'avatar') dicoManager('setdefault() --> add a pair (key, value) with the corresponding parameter if the key is not already in the dictionary.', myDico) myDico = {'john': 'smith', 'sarah': 'connors', 'darth': 'vader' } print('Elements present in myDico:') print(str(myDico) + '\n') print('Elements present in myDico2:') print(str(myDico2) + '\n') print('Elements present in myDico3:') print(str(myDico3) + '\n') myDico.update(myDico3) dicoManager('update() --> add all pairs (key, value) of myDico3 in myDico and if there is a same key, the new pair (key, value) replaces the old.', myDico) myDico = myDico.values() dicoManager('values() --> retrieve all values of the dictionary.', myDico)
copy() --> copy the dictionary into another one. {'sarah': 'connors', 'john': 'smith', 'darth': 'vader'} clear() --> clear all the dictionary. {} Elements in "myDico". {'sarah': 'connors', 'john': 'smith', 'darth': 'vader'} fromkeys() --> change all values from myDico2 to a list of items. {'sarah': ['hello', 'world'], 'john': ['hello', 'world'], 'darth': ['hello', 'world']} get() --> retrieve the value associates with the key "sarah", in our case: "connors". items() --> retrieve all pairs (key, value) of the dictionary. dict_items([('sarah', 'connors'), ('john', 'smith'), ('darth', 'vader')]) keys() --> retrieve all keys of the dictionary. dict_keys(['sarah', 'john', 'darth']) pop() --> remove the key and value corresponding at the key passed in parameter and return the value. connors Elements present in myDico after pop(): {'john': 'smith', 'darth': 'vader'} popitem() --> remove the first pair (key, value) of the dictionary. {'darth': 'vader'} setdefault() --> add a pair (key, value) with the corresponding parameter if the key is not already in the dictionary. {'sarah': 'lol', 'film': 'avatar', 'hifi': 'tv', 'room': 'kitchen', 'bed': 'king size'} Elements present in myDico: {'sarah': 'connors', 'john': 'smith', 'darth': 'vader'} Elements present in myDico2: connors Elements present in myDico3: {'sarah': 'lol', 'hifi': 'tv', 'room': 'kitchen', 'bed': 'king size'} update() --> add all pairs (key, value) of myDico3 in myDico and if there is a same key, the new pair (key, value) replaces the old. {'sarah': 'lol', 'hifi': 'tv', 'room': 'kitchen', 'john': 'smith', 'darth': 'vader', 'bed': 'king size'} values() --> retrieve all values of the dictionary. dict_values(['lol', 'tv', 'kitchen', 'smith', 'vader', 'king size'])
You made it. Great job!
Add new comment