Python Menu

Dictionary Sequence in Python


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 {}.


Syntax:
Dict={key1:value1, key2:value2, …….}

Example:
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}  
print(student)
Output:
{'Name': 'Kiran', 'Age': 22, 'Regno': 562, 'Branch': 'CSE'}

Accessing the dictionary values

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.

Example:
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"])
Output:
Name :  Kiran
Age :  22
RegNo :  562
Branch :  CSE

Updating dictionary values

The dictionary is a mutable data type, and its values can be updated by using the specific keys.

Example:
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
print("printing student data .... ")
print(student)
student["Name"]="Kishore"
print("printing updated data .... ")
print(student)
Output:
printing student data ....
{'Name': 'Kiran', 'Age': 22, 'Regno': 562, 'Branch': 'CSE'}
printing updated data ....
{'Name': 'Kishore', 'Age': 22, 'Regno': 562, 'Branch': 'CSE'}

Deleting dictionary values

The items of the dictionary can be deleted by using the del keyword.

Example:
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)
Output:
printing student data ....
{'Name': 'Kiran', 'Age': 22, 'Regno': 562, 'Branch': 'CSE'}
printing the modified information
{'Name': 'Kiran', 'Age': 22, 'Regno': 562}

Accessing the dictionary values using loops

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

Example:
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
print("Keys are :")
for x in student:
	print(x)
Output:
Keys are :
Name
Age
Regno
Branch

print all the values of a dictionary

Example:
print("values are :")
for x in student:
	print(student[x])
Output:
values are :
Kiran
22
562
CSE

print all the Keys & values of a dictionary

Example:
print("Key and values are :")
for x,y in student.items():
	print(x,y)
Output:
Key and values are :
Name Kiran
Age 22
Regno 562
Branch CSE

Dictionary Functions

Python supports following in-bulit functions, Those are

  • len()
  • copy()
  • get()
  • keys()
  • items()
  • values()
  • update()
  • pop()
  • clear()

☞ len()

In python, len() function is used to find length of given dictionary.

Syntax:
len(dictionary)

Example:
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
print("Length of Dictionary is:",len(student))
Output:
Length of Dictionary is: 4

☞ copy()

It returns another copy of given dictionary.

Syntax:
dictionary.copy()

Example:
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
student2=student.copy()
print(student2)
Output:
{'Name': 'Kiran', 'Age': 22, 'Regno': 562, 'Branch': 'CSE'}

☞ get()

In python, get() is used to get the value of specified key form dictionary.

Syntax:
dictionary.get()

Example:
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
print("Name is :",student.get("Name"))
print("RegNo is :",student.get("Regno"))
Output:
Name is : Kiran
RegNo is : 562

☞ keys()

In python keys() method is used to fetch all the keys from the dictionary

Syntax:
dictionary.keys()

Example:
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
for x in student.keys():
	print(x)
Output:
Name
Age
Regno
Branch

☞ items()

In python items() method returns a new view of the dictionary. This view is collection of key value tuples.

Syntax:
dictionary.items()

Example:
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
for x in student.items():
	print(x)
Output:
('Name', 'Kiran')
('Age', 22)
('Regno', 562)
('Branch', 'CSE')

☞ values()

In python values() method is used to collect all the values from a dictionary.

Syntax:
Dictionary.values()

Example:
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
for x in student.values():
	print(x)
Output:
Output:
Kiran
22
562
CSE

☞ update()

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,….})

Example:
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
student.update({"Regno":590})
student.update({"phno":56895})
print(student)
Output:
{'Name': 'Kiran', 'Age': 22, 'Regno': 590, 'Branch': 'CSE', 'phno': 56895}

☞ pop()

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.

Syntax:
Dictionary.remove(key)

Example:
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
student.pop('Age')
print(student)
student.pop('hallno')
print(student)
Output:
{'Name': 'Kiran', 'Regno': 562, 'Branch': 'CSE'}
KeyError: 'hallno'

☞ clear()

In python, clear() is used to delete all the items of the dictionary.

Syntax:
Dictionary.clear()

Example:
student = {"Name": "Kiran", "Age": 22, "Regno":562,"Branch":"CSE"}
print(student)
student.clear()
print(student)
Output:
{'Name': 'Kiran', 'Age': 22, 'Regno': 562, 'Branch': 'CSE'}
{}

Next Topic :Files in Python