Python Dictionary 5

How to delete or remove elements from a dictionary?

pop():

we can remove a particular item in a dictionary by using the method pop(). this method removes as item with the provided key and returns the value.

Example of pop():

Example:1

# create a dictionary
squares = {1:1, 2:4, 3:9, 4:16, 5:25}
print(squares)

# remove a particular item
print(squares.pop(4))
print(squares)

print(squares.pop(2))
print(squares)

Output:

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
16
{1: 1, 2: 4, 3: 9, 5: 25}
4
{1: 1, 3: 9, 5: 25}

# create a dictionary
squares = {1:1, 2:4, 3:9, 4:16, 5:25}  
print(squares)

# remove a particular item
print(squares.pop(4))  
print(squares)

print(squares.pop(2))  
print(squares)

Output:

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
16
{1: 1, 2: 4, 3: 9, 5: 25}
4
{1: 1, 3: 9, 5: 25}

Example:2

#student details

student={“roll”:101,”name”:”sumit”,”per”:98}
print(student)
# remove a particular item
print(student.pop(“name”))
print(student)
# remove a particular item
print(student.pop(“per”))
print(student)

Output:

{‘roll’: 101, ‘name’: ‘sumit’, ‘per’: 98}
sumit
{‘roll’: 101, ‘per’: 98}
98
{‘roll’: 101}
>>>

#student details

student={"roll":101,"name":"sumit","per":98}
print(student)
# remove a particular item
print(student.pop("name"))
print(student)
# remove a particular item
print(student.pop("per"))
print(student)

Output:

{'roll': 101, 'name': 'sumit', 'per': 98}
sumit
{'roll': 101, 'per': 98}
98
{'roll': 101}
>>> 

popitem()

the method, popitem() can be used to remove and return an arbitrary item (key, value) form the dictionary.

Example of popitem():

Example:1

# create a dictionary
squares = {1:1, 2:4, 3:9, 4:16, 5:25}
print(squares)

# remove a particular item
print(squares.popitem())
print(squares)

print(squares.popitem())
print(squares)

Output:

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
(5, 25)
{1: 1, 2: 4, 3: 9, 4: 16}
(4, 16)
{1: 1, 2: 4, 3: 9}

# create a dictionary
squares = {1:1, 2:4, 3:9, 4:16, 5:25}  
print(squares)

# remove a particular item
print(squares.popitem())  
print(squares)

print(squares.popitem())  
print(squares)

Output:

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
(5, 25)
{1: 1, 2: 4, 3: 9, 4: 16}
(4, 16)
{1: 1, 2: 4, 3: 9}

Example:2

#student details

student={“roll”:101,”name”:”sumit”,”per”:98}
print(student)
# remove a particular item
print(student.popitem())
print(student)
# remove a particular item
print(student.popitem())
print(student)

Output:

{‘roll’: 101, ‘name’: ‘sumit’, ‘per’: 98}
(‘per’, 98)
{‘roll’: 101, ‘name’: ‘sumit’}
(‘name’, ‘sumit’)
{‘roll’: 101}
>>>

#student details

student={"roll":101,"name":"sumit","per":98}
print(student)
# remove a particular item
print(student.popitem())
print(student)
# remove a particular item
print(student.popitem())
print(student)

Output:

{'roll': 101, 'name': 'sumit', 'per': 98}
('per', 98)
{'roll': 101, 'name': 'sumit'}
('name', 'sumit')
{'roll': 101}
>>> 

clear()

all the items can be removed at once using the clear() method.

Example of clear():

Example:1

# create a dictionary
squares = {1:1, 2:4, 3:9, 4:16, 5:25}
print(squares)
print(squares.clear())
print(squares)

Output:

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
None
{}
>>>

# create a dictionary
squares = {1:1, 2:4, 3:9, 4:16, 5:25}  
print(squares)
print(squares.clear())  
print(squares)

Output:

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
None
{}
>>> 

Example:2

#student details

student={“roll”:101,”name”:”sumit”,”per”:98}
print(student)
print(student.clear())
print(student)

Output:

{‘roll’: 101, ‘name’: ‘sumit’, ‘per’: 98}
None
{}
>>>

#student details

student={"roll":101,"name":"sumit","per":98}
print(student)
print(student.clear())
print(student)
{'roll': 101, 'name': 'sumit', 'per': 98}
None
{}
>>> 

del

we can also use the del keyword to remove individual items or the entire dictionary itself.

Example of del:

Example:1

# create a dictionary
squares = {1:1, 2:4, 3:9, 4:16, 5:25}
print(squares)
del squares
print(squares)
#error will be displayed as dictionary is deleted (not present)

# create a dictionary
squares = {1:1, 2:4, 3:9, 4:16, 5:25}  
print(squares)
del squares  
print(squares)
#error will be displayed as dictionary is deleted (not present)

Output:

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Traceback (most recent call last):
  File "E:\python\Dictionary\py_dic1.py", line 5, in <module>
    print(squares)
NameError: name 'squares' is not defined
>>> 

Example:2

#student details

student={“roll”:101,”name”:”sumit”,”per”:98}
print(student)
del student
print(student)
#error will be displayed as dictionary is deleted (not present)

Output:

{'roll': 101, 'name': 'sumit', 'per': 98}
Traceback (most recent call last):
  File "E:\python\Dictionary\py_dic1.py", line 7, in <module>
    print(student)
NameError: name 'student' is not defined
>>>