You are on page 1of 15

BrightCHAMPS

Project:Dictionary in python
OBJECTIVE

Aim Platform

dictionary
https://www.onlin
egdb.com/ .and
https://colab.rese
arch.google.com/
Introduction: We have already learned about Dictionary, How to use them for different
Data type, different functions of dictionary, accessing and changing dictionary. Now,
Let’s learn about other functions of dictionary.
Dictionary Functions like adding, removing and changing. Loop, Copy and Nested Dictionaries.

Add Dictionary: Adding an item to the dictionary is done by using a new index key and assigning a
value.

# Add items in Dictionary: Adding an item to the dictionary is done by using a


new index key and assigning a value.
dictRollNo={
1:'Rahul', Output:
2:'Subhash', {1: 'Rahul', 2: 'Subhash', 3: 'Diya', 4: 'Riha', 5: 'John',
3:'Diya', '6': 'Akash'}
4:'Riha',
5:'John'
}
dictRollNo["6"]="Akash"
print(dictRollNo)
# Remove items in Dictionary: Removing an item from the dictionary is done by using pop()
function.
# Let's remove 3rd element from dictionary.

dictRollNo={
1:'Rahul',
2:'Subhash',
3:'Diya',
4:'Riha',
5:'John'
}
dictRollNo.pop(3) # Here we removed the third key.
print(dictRollNo)

Output:

{1: 'Rahul', 2: 'Subhash', 4: 'Riha', 5: 'John'}


# popitem(). It removes the last element.
dictRollNo={
1:'Rahul',
2:'Subhash',
3:'Diya',
4:'Riha',
5:'John'
}
dictRollNo.popitem() # Here we removed the last element.
print(dictRollNo)

Output:

{1: 'Rahul', 2: 'Subhash', 3: 'Diya', 4: 'Riha'}


del : It deletes the dictionary completely.

dictRollNo={
1:'Rahul',
2:'Subhash',
3:'Diya',
4:'Riha',
5:'John'
}
del dictRollNo # It will remove the dictionary completely.
print(dictRollNo) #It will show an error message.
clear(): It clears all elements of dictionary but not dictionary.

dictRollNo={
1:'Rahul',
2:'Subhash',
3:'Diya',
4:'Riha',
5:'John'
}
dictRollNo.clear() # This clears all elements of dictionary but dictionary
remains.
print(dictRollNo)

Output:
{}
update(): It helps in updating and adding new element.

dictRollNo={
1:'Rahul',
2:'Subhash',
3:'Diya',
4:'Riha',
5:'John'
}
dictRollNo.update({'6':'Shishir'}) # Update it helps in adding a new element in dictionary and can
also be used for changing element.
print(dictRollNo)

Output:

{1: 'Rahul', 2: 'Subhash', 3: 'Diya', 4: 'Riha', 5: 'John', '6': 'Shishir'}


# Let’s start putting loop and see different conditions.

dictRollNo={
1:'Rahul',
2:'Subhash',
3:'Diya',
4:'Riha',
5:'John'
}
for x in dictRollNo:
print(x) # it will print all the keys

for x in dictRollNo:
print(dictRollNo[x]) # it will print all values

for x in dictRollNo.keys():
print(x) # it will print all keys

for x in dictRollNo.values():
print(x) # it will print all values

for x in dictRollNo.items():
print(x) # it will print both keys and values
Output:

1
2
3
4
5
Rahul
Subhash
Diya
Riha
John
1
2
3
4
5
Rahul
Subhash
Diya
Riha
John
(1, 'Rahul')
(2, 'Subhash')
(3, 'Diya')
(4, 'Riha')
(5, 'John')
# Let's make a copy of the dictionary.
dictRollNo={
1:'Rahul',
2:'Subhash',
3:'Diya',
4:'Riha',
5:'John'
}
#dictRollNo=dictRollNo1 # it will show an error because you can't copy dictionary like
this.

dictRollNoCopy = dictRollNo.copy()
print(dictRollNoCopy)

dictRollNoCopy1=dict(dictRollNo)
print(dictRollNoCopy1)

Output:

{1: 'Rahul', 2: 'Subhash', 3: 'Diya', 4: 'Riha', 5: 'John'}


{1: 'Rahul', 2: 'Subhash', 3: 'Diya', 4: 'Riha', 5: 'John'}
# Nested Dictionary: A dictionary inside a dictionary is called Nested Dictionary.
# Let's make dictionary of students with their details.
# Here, we have saved data of students with their name, class and gender.
# The way we are writing structure is more visible to read.
# Generally data is saved this way only.
students= {
"student1" :{
"name":"Anil", Output:
"class": "4",
"gender":"Male"
{'student1': {'name': 'Anil', 'class': '4', 'gender': 'Male'},
'student2': {'name': 'Akash', 'class': '8', 'gender': 'Male'},
}, 'student3': {'name': 'Avani', 'class': '9', 'gender': 'Female'}}
"student2" :{
"name":"Akash",
"class": "8",
"gender":"Male"
},
"student3" :{
"name":"Avani",
"class": "9",
"gender":"Female"
},
}
print(students)
# Class Assignment:Please make a nested dictionary with the roll no as keys, save name and
class in values.
# 1. Then, print it.
# 2. Add data of 2 more students.
# 3. Remove data of last student.
# 4. Print all values using loop.
# 5. Change value of any one element.
# Homework Assignment:
# Make a nested dictionary.
# 1. Make a copy of it.
# 2. Access any two element from inside.
# 3. Access all keys of it using loop
# 4. Clear and then delete the dictionary.

You might also like