In python a dictionary is the collection of key-value pairs where the value can be any python object whereas the keys are the immutable python object, i.e., Numbers, string or tuple.
The dictionary can be created by using multiple key-value pairs which are separated by comma(,) and enclosed within the curly braces {}.
Dict={key1:value1, key2:value2, …….}
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
print(student)
{'Name': 'Kiran', 'Age': 22, 'Regno': 562, 'Branch': 'CSE'}
The data can be accessed in the list and tuple by using the indexing.
However, the values can be accessed in the dictionary by using the keys as keys are unique in the dictionary.
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
print("Name : ",student["Name"])
print("Age : ",student["Age"])
print("RegNo : ",student["Regno"])
print("Branch : ",student["Branch"])
Name : Kiran
Age : 22
RegNo : 562
Branch : CSE
The dictionary is a mutable data type, and its values can be updated by using the specific keys.
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
print("printing student data .... ")
print(student)
student["Name"]="Kishore"
print("printing updated data .... ")
print(student)
printing student data ....
{'Name': 'Kiran', 'Age': 22, 'Regno': 562, 'Branch': 'CSE'}
printing updated data ....
{'Name': 'Kishore', 'Age': 22, 'Regno': 562, 'Branch': 'CSE'}
The items of the dictionary can be deleted by using the del keyword.
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
print("printing student data .... ")
print(student)
del student["Branch"]
print("printing the modified information ")
print(student)
printing student data ....
{'Name': 'Kiran', 'Age': 22, 'Regno': 562, 'Branch': 'CSE'}
printing the modified information
{'Name': 'Kiran', 'Age': 22, 'Regno': 562}
A dictionary can be iterated using the for loop.We can able to access only keys, only values and both keys&values.
print all the keys of a dictionary
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
print("Keys are :")
for x in student:
print(x)
Keys are :
Name
Age
Regno
Branch
print all the values of a dictionary
print("values are :")
for x in student:
print(student[x])
values are :
Kiran
22
562
CSE
print all the Keys & values of a dictionary
print("Key and values are :")
for x,y in student.items():
print(x,y)
Key and values are :
Name Kiran
Age 22
Regno 562
Branch CSE
Python supports following in-bulit functions, Those are
In python, len() function is used to find length of given dictionary.
Syntax:len(dictionary)
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
print("Length of Dictionary is:",len(student))
Length of Dictionary is: 4
It returns another copy of given dictionary.
Syntax:dictionary.copy()
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
student2=student.copy()
print(student2)
{'Name': 'Kiran', 'Age': 22, 'Regno': 562, 'Branch': 'CSE'}
In python, get() is used to get the value of specified key form dictionary.
Syntax:dictionary.get()
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
print("Name is :",student.get("Name"))
print("RegNo is :",student.get("Regno"))
Name is : Kiran
RegNo is : 562
In python keys() method is used to fetch all the keys from the dictionary
Syntax:dictionary.keys()
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
for x in student.keys():
print(x)
Name
Age
Regno
Branch
In python items() method returns a new view of the dictionary. This view is collection of key value tuples.
Syntax:dictionary.items()
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
for x in student.items():
print(x)
('Name', 'Kiran')
('Age', 22)
('Regno', 562)
('Branch', 'CSE')
In python values() method is used to collect all the values from a dictionary.
Syntax:Dictionary.values()
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
for x in student.values():
print(x)
Output:
Kiran
22
562
CSE
In python update() method updates the dictionary with the key and value pairs. It inserts key/value if it is not present. It updates key/value if it is already present in the dictionary.
Syntax:Dictionary.update({key:value,….})
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
student.update({"Regno":590})
student.update({"phno":56895})
print(student)
{'Name': 'Kiran', 'Age': 22, 'Regno': 590, 'Branch': 'CSE', 'phno': 56895}
In python pop() method removes an element from the dictionary. It removes the element which is associated to the specified key.
If specified key is present in the dictionary, it remove and return its value.
If the specified key is not present, it throws an error KeyError.
Dictionary.remove(key)
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
student.pop('Age')
print(student)
student.pop('hallno')
print(student)
{'Name': 'Kiran', 'Regno': 562, 'Branch': 'CSE'}
KeyError: 'hallno'
In python, clear() is used to delete all the items of the dictionary.
Syntax:Dictionary.clear()
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
print(student)
student.clear()
print(student)
{'Name': 'Kiran', 'Age': 22, 'Regno': 562, 'Branch': 'CSE'}
{}