Python Dictionary
Hi, welcome to the Python Dictionary tutorial, This is the part 5 of our Python tutorials. Before we dive into the learning the dictionaries, we should know what is the dictionary. In dictionary we have two attributes, key and value, like the physical dictionary, key is the word that we want to be translated and value is translation of the key. For instance in English dictionary if our key would be “power” our value is “strength”, in Python we put a key and define a value for that key. If you do not have the proper tools to code python, click on the button down below to code python more easily.
Basics of Python Dictionary
Now that we know what is Python Dictionary is, we could start to create one. For creating one you should use “{}” then assign a key, use “:” and assign its value. In down below I have created a dictionary that contains a personal information of imaginary person.
Tips: after assigning the key and value remember to use “,” the reason that you should do this is to separate the one pair of key and value form another one. The second tip is you do not need to write every key and value in multiple lines you could use just one line (but I strongly recommend you to use multiple lines because it helps other people to read your code more efficiently).
#Python Dictionary
my_dictionary = {
"name": "Erick",
"age": "32",
"career": "software developer",
"pet": "dog"
}
Calling the Python Dictionary
How we call the Dictionary? Like the lists we should use “my_dictionary[]” but there is one difference, in list we would put the index of the element but here we put the key of the dictionary and it will return the key’s value. For example in down below we call the “name” and it will return the Erick, or we call the pet and it will return the dog.
The other method that we use for calling is the “.get” firs value we put in is the dictionary’s key and the second one would show up if the key is not exist in the dictionary. For example in down below we put the career and it will return “software developer” but in the second one we put the “weight” which does not exist in my_dictionary and it will return “key is not valid”.
#Calling the Dictionary
my_dictionary = {
"name": "Erick",
"age": "32",
"career": "sofware developer",
"pet": "dog"
}
print(my_dictionary["name"])
print(my_dictionary["pet"])
print(my_dictionary.get("career", "key is not valid"))
print(my_dictionary.get("weight", "key is not valid"))
print(my_dictionary.keys()) #returns all of the keys of my_dictionary
print(my_dictionary.values()) #returns of all of the values of my_dictionary
print(my_dictionary.items()) #reutns all of the items of my_dictionary
Modifying the Dictionary
There is quite a lot of ways to do the modification for dictionary in python, the easiest one is: “my_dictioanry[“name”] = “elena” it will change the name’s value from Erick to Elena if the key that you put in “[]” is not one of the dictionary’s keys it will add the key and its value to the dictionary. Other useful methods are “.update” which does the same thing as the first one, “.pop” delete the specified key and its value, “.popitem” delete the last pair of value and key.
If you want to clear it use “.clear” and if you want to copy it use”.copy”.
#Python Dictionary
my_dictionary = {
"name": "Erick",
"age": "32",
"career": "sofware developer",
"pet": "dog"
}
my_dictionary["name"] = "elena"
my_dictionary.update({"car": "sedan"}) #updates the value of the specified key if key exists in dictionary otherwise add the key and value
my_dictionary.pop("pet")
del my_dictionary["pet"] #does the same thing as .pop("pet") delete the pet key and value
my_dictionary.popitem() #delete last value and key pair of the my_dictionary
my_dictionary2 = my_dictionary.copy() #takes a copy of my_dictionary
my_dictionary.clear() #clears all of the item in my_dictionary