In this tutorial, we will learn about the dictionary in Python. Here we will have many examples to create, insert, update and delete an element from the dictionary.
What is a Dictionary
- It is an unordered collection of elements.
- Each element in the Dictionary has key and value.
- Key and value pairs are separated with a colon ( : ).
- Keys must be unique but values can be duplicated.
- Dictionary is mutable, so we can change elements.
How to Create a Dictionary
We can create a dictionary in Python by placing all the items inside curly { } brackets separated with a comma. Each item must-have key and value left side of a colon (:) is key and other side is value. Or we can use dict() method to create a dictionary.
#empty dictionary
product={}
#dictionary with integer keys
product={1001:"Laptop", 1002:"Mobile", 1003:"Camera"}
#dictionary with text keys
product={"Laptop":1001, "Mobile":1002, "Camera":1003}
#dictionary with mixed type of keys
student={"name" : "John", "marks" : [73, 66, 71]}
#list to dictionary using dict().
items=[ ("Laptop",1001), ("Mobile",1002), ("Camera",1003) ]
product=dict(items)
Access Element of the Dictionary
We can access an element of the dictionary by referring keys in square [] brackets or using get() method of dictionary in Python.
#create a dictionary
product={"name":"Laptop", "code":1001}
#print whole dictionary
print(product)
#using keys in square brackets
print(product["name"]) #output : laptop
print(product["code"]) #output : 1001
#using get() method of dictionary
print(product.get("name")) #output : laptop
print(product.get("code")) #output : 1001
Loop Through a Dictionary
We can access elements of the dictionary using for loop also.
#create a dictionary
product={1001:"Laptop", 1002:"Mobile", 1003:"Camera"}
#loop through a dictionary, return key only.
for k in product:
print(k)
#loop through a dictionary, print key and value.
for k in product:
print(k,"-",product[k])
#using values() method to return value only.
for v in product.values():
print(v)
#using items() method to return key and value
for k,v in product.items():
print(k,"-",v)
Update Dictionary in Python
Using Assignment ( = ) Operator
Dictionary is mutable, so we can add a new item or change the existing item using assignment ( = ) operator.
#create a dictionary
product={1001:"Laptop", 1002:"Mobile"}
#--------------------------------
#add new item in the dictionary
product[1003]="Camera"
print(product)
#output: {1001: 'Laptop', 1002: 'Mobile', 1003: 'Camera'}
#--------------------------------
#change existing item in a dictionary
product[1002]="Tablet"
print(product)
#output: {1001: 'Laptop', 1002: 'Tablet', 1003: 'Camera'}
Using update() Method
Python dictionary has update() method to append new item or change existing item. We can also add two dictionaries in Python using the update() method.
#create a dictionary
product={1001:"Laptop", 1002:"Mobile"}
#--------------------------------
#add new item in dictionary
product.update({1003:"Camera"})
print(product)
#--------------------------------
#Add two dictionary
dic1={"A":1, "B":2, "C":3}
dic2={"X":24, "Y":25, "Z":26}
dic1.update(dic2)
print(dic1)
Delete an Item from the Dictionary
In Python we can delete items from the dictionary using the del keyword or using pop(), popitem(), or clear() method.
Using del Keyword
In Python, we can delete an element or entire dictionary using the del keyword.
dic={"A":1, "B":2, "C":3}
#delete an item using key
del dic["B"]
print(dic)
#delete entire dictionary
del dic
print(dic) #output : error
Remove Dictionary Using Method
- pop() – Remove an item of the given key.
- popitem() – Remove last inserted item (before version 3.7, remove any random item).
- clear() – Remove all the items of the dictionary.
dic={"A":1, "B":2, "C":3}
#delete an item using key
dic.pop("B")
print(dic)
#delete an item
dic.popitem()
print(dic)
#remove all items of the dictionary
dic.clear()
print(dic)