Python dictionary is the collection of unordered changeable indexed represented by curly braces, having keys and values.
For Introduction to Python: Click here
Create a dictionary
dict = {
"brand": "Realme",
"model": "X50 pro",
"year": 2020
}
print(dict)
Here the key names are “brand”, “model”, “year” and the corresponding key values are “Realme”, “X50 pro”, “2020”
Accessing Items
To access items we use the get() method.
x = dict.get("year") #returns 2020
Change Values
By referring to the key names we can change the values.
dict = {
"brand": "Realme",
"model": "X50 pro",
"year": 2020
}
print(dict)
dict["year"]=2022
print(dict)
Dictionary Length
We use the len() method for this purpose
dict = {
"brand": "Realme",
"model": "X50 pro",
"year": 2020
}
print(len(dict)) #returns 3
Adding Items
Adding an item to the dictionary is done by using a new index key and assigning a value to it:
dict = {
"brand": "Realme",
"model": "X50 pro",
"year": 2020
}
print(dict)
dict["RAM"]="6 GB"
print(dict)
Removing Items
dict = {
"brand": "Realme",
"model": "X50 pro",
"year": 2020
}
dict.pop("year")
print(dict) # returns {'brand': 'Realme', 'model': 'X50 pro'}
In the above code, we use pop() method for removing key.
dict = {
"brand": "Realme",
"model": "X50 pro",
"year": 2020
}
dict.popitem()
print(dict) #returns {'brand': 'Realme', 'model': 'X50 pro'}
If no key name is mentioned the method popitem() will remove the last inserted item.
del() method as well as clear() method completely delete the dictionary
Loop Through a Dictionary
Method to returns the key name is for loop
dict = {
"brand": "Realme",
"model": "X50 pro",
"year": 2020
}
for i in dict:
print(i)
Method to print the key values is :
dict = {
"brand": "Realme",
"model": "X50 pro",
"year": 2020
}
for i in dict:
print(dict[i])
In order to determine both key name and values, the method item() is used.
dict = {
"brand": "Realme",
"model": "X50 pro",
"year": 2020
}
for x, y in dict.items():
print(x, y)
Copy a Dictionary
You cannot copy a dictionary simply by typing dict2() = dict1(), because:
dict2() will only be a reference to dict1(), and changes made in dict1() will automatically also be made in dict2(). The built-in method is copy().
dict1 = {
"brand": "Realme",
"model": "X50 pro",
"year": 2020
}
mydict = dict1.copy()
print(mydict)
The built-in method() is used in Python dictionary is dict()
dict1 = {
"brand": "Realme",
"model": "X50 pro",
"year": 2020
}
mydict = dict(dict1)
print(mydict)
Nested Dictionaries
A dictionary that consists of many dictionaries within it is known as a nested dictionary.
products = {
"phone1" : {
"brand" : "Realme",
"year" : 2020
},
"phone2" : {
"brand" : "HONOR 8x",
"year" : 2019
},
"phone3" : {
"name" : "Redmi Note 5 pro",
"year" : 2017
}
}
print(products)
The dict() Constructor
It is also possible to use the dict() constructor to make a new dictionary:
thisdict = dict(brand="Ford", model="Mustang", year=1964)
print(thisdict)
Dictionary Methods
Python has so many Python built-in methods. They are:
- clear()
- copy()
- gets()
- items()
- keys()
- pop()
- popitem()
- setdefault()
- update()
- values()