- A dictionary is a collection which is unordered, changeable and indexed. In Python , dictionaries are written with curly brackets {} , and they have keys and values.
- Dictionaries are mutable. i.e. it is possible to add, modify, delete key-value pairs.
- The key should be unique and can be of any data type.
thisdict = {
"Company": "Maruti",
"model": "Swift",
"year": 2020
}
print(thisdict)
{'Company': 'Maruti', 'model': 'Swift', 'year': 2020}
-
In dictionary, we cannot store multiple values for the same keys. If we pass more than one value for a single key, then the value which is last assigned is considered as the value of the key.
-
In python, the key cannot be any mutable object. We can use numbers, strings, or tuples as the key, but we cannot use any mutable object like the list as the key in the dictionary.
thisdict = {
"Company": "Maruti",
"model": "Swift",
"year": 2020,
"model": "WagonR" #then the value which is last assigned is considered as the value of the key.
}
print(thisdict)
{'Company': 'Maruti', 'model': 'WagonR', 'year': 2020}
- You can access the items of a dictionary by referring to its key name, inside square brackets:
Get the value of the "model" key:
thisdict = {
"Company": "Maruti",
"model": "Swift",
"year": 2020
}
x = thisdict["model"]
print(thisdict)
Swiftt
There is also a method called get() that will give you the same result:
thisdict = {
"Company": "Maruti",
"model": "Swift",
"year": 2020
}
x = thisdict.get("model")
print(x)
Swiftt
- You can change the value of a specific item by referring to its key name:
thisdict = {
"Company": "Maruti",
"model": "Swift",
"year": 2020
}
thisdict["year"] = 2018
print(thisdict)
{'Company': 'Maruti', 'model': 'Swift', 'year': 2018}
- You can loop through a dictionary by using a for loop.
- When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well.
thisdict = {
"Company": "Maruti",
"model": "Swift",
"year": 2020
}
for x in thisdict:
print(x)
Company
model
year
thisdict = {
"Company": "Maruti",
"model": "Swift",
"year": 2020
}
for x in thisdict:
print(thisdict[x])
Maruti
Swift
2020
thisdict = {
"Company": "Maruti",
"model": "Swift",
"year": 2020
}
for x in thisdict.values():
print(x)
Maruti
Swift
2020
thisdict = {
"Company": "Maruti",
"model": "Swift",
"year": 2020
}
for x in thisdict.values():
print(x)
Maruti
Swift
2020
thisdict = {
"Company": "Maruti",
"model": "Swift",
"year": 2020
}
for x, y in thisdict.items():
print(x, y)
Company Maruti
year 2020
model Swift
- To determine how many items (key-value pairs) a dictionary has, use the len() function.
thisdict = {
"Company": "Maruti",
"model": "Swift",
"year": 2020
}
print(len(thisdict))
3
- Adding an item to the dictionary is done by using a new index key and assigning a value to it:
thisdict = {
"Company": "Maruti",
"model": "Swift",
"year": 2020
}
thisdict["color"] = "blue"
print(thisdict)
{'Company': 'Maruti', 'model': 'Swift', 'year': 2020, 'color': 'blue'}
- There are several methods to remove items from a dictionary:
thisdict = {
"Company": "Maruti",
"model": "Swift",
"year": 2020
}
thisdict.pop("model")
print(thisdict)
{'Company': 'Maruti', 'year': 2020}
thisdict = {
"Company": "Maruti",
"model": "Swift",
"year": 2020
}
thisdict.popitem()
print(thisdict)
{'Company': 'Maruti', 'model': 'Swift'}
thisdict = {
"Company": "Maruti",
"model": "Swift",
"year": 2020
}
del thisdict["model"]
print(thisdict)
{'Company': 'Maruti', 'year': 2020}
thisdict = {
"Company": "Maruti",
"model": "Swift",
"year": 2020
}
del thisdict
print(thisdict) #this will cause an error because "thisdict" no longer exists.
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
NameError: name 'thisdict' is not defined
thisdict = {
"Company": "Maruti",
"model": "Swift",
"year": 2020
}
thisdict.clear()
print(thisdict)
{}
thisdict = {
"Company": "Maruti",
"model": "Swift",
"year": 2020
}
mydict = thisdict.copy()
print(mydict)
{'Company': 'Maruti', 'model': 'Swift', 'year': 2020}
Methods | Description |
---|---|
clear() | Removes all items from the dictionary. |
copy() | Returns a shallow copy of the dictionary. |
fromkeys(seq[, v]) | Returns a new dictionary with keys from seq and value equal to v (defaults to None). |
get(key[,d]) | Returns the value of the key. If the key does not exist, returns d (defaults to None). |
items() | Return a new object of the dictionary's items in (key, value) format. |
keys() | Returns a new object of the dictionary's keys. |
pop(key[,d]) | Removes the item with the key and returns its value or d if key is not found. If d is not provided and the key is not found, it raises KeyError. |
popitem() | Removes and returns an arbitrary item (key, value). Raises KeyError if the dictionary is empty. |
setdefault(key[,d]) | Returns the corresponding value if the key is in the dictionary. If not, inserts the key with a value of d and returns d (defaults to None). |
update([other]) | Updates the dictionary with the key/value pairs from other, overwriting existing keys. |
values() | Returns a new object of the dictionary's values |