Python dictionaries are other types of data structures like lists or tuples but they function in a very specific way: they are indexed by keys and their respective values.
We can initialize the key value pair in the below format,
Or we can initialize it incrementally,
A tuple can also be a dictionary key, because tuples are immutable:
So elements can be added that way too.
Has length 5 and can be indexed like this:
key |
value |
"brand" |
"Toyota" |
"color" |
"black" |
"model" |
"Yaris" |
Output:
We can iterate over the list created by Dict1.keys() and get the keys:
Output:
We can iterate over the list created by dict1.values() and get the values:
Output:
We can iterate over the list of tuples created by dict1.items() and get the pair key-value:
Output:
There is a way to finding out if a specific key exists in a specific dictionary but only works for keys. Example:
This will print out True because the key “brand” indeed exists in Dict1. Example:
This will print out True because the value “Toyota” key indeed exists in dict1. Example:
This will print out True because the pair (“brand”,” Toyota”) indeed exists in dict1.
Dictionaries and lists share the following characteristics:
• Both are mutable.
• Both are dynamic. They can grow and shrink as needed.
• Both can be nested. A list can contain another list. A dictionary can contain another dictionary. A dictionary can also contain a list, and vice versa.
Dictionaries differ from lists primarily in how elements are accessed:
• List elements are accessed by their position in the list, via indexing.
• Dictionary elements are accessed via keys.
Function |
Description |
E.g |
dict.clear() |
It removes all the elements. |
Dict1.clear() clears the entire dictionary Dict1 is now {} (an empty dictionary) |
dict.get(key) |
Returns the value of a specific key. |
Dict1.get("color") will return the String "black" |
dict.items() |
Returns a list of tuples containing the key-value pairs in |
Dict1.items() returns the list of tuples: [("brand", "Toyota"), ("color", "black"), ("model", "Yaris")] |
dict.keys() |
Returns a list of all keys in |
Dict1.keys() will return the list: ["brand","color","model"] |
dict.values() |
Returns a list of all values in |
Dict1.values() will return the list: ["Toyota","black","Yaris"] |
dict.Pop(key) |
Removes key-value pair with specific key. Much like remove in lists or tuples. |
Dict.pop("brand") will remove the key-value pair with the key "brand" |
Dict.popitem() |
It removes and returns the last object of the dict as a tuple. |
print(Dict1.popitem())removes the pair "brand" : "Yaris" and returns the tuple ("brand", "Yaris"). |
dict.update(obj) |
If obj is of type dictionary then dict will be added that dictionary. If a key-value pair already exists then that value for that key is updated. |
Dict1.update({"color" : "white", "autopilot" : false}) will update the value of the color to white and add the field autopilot |
dict.clear():
dict.get(key):
dict.items():
dict.keys():
dict.values():
dict.Pop(key):
dict.popitem():
dict.update(obj):