Python program for removing elements from a dictionary

Here, we are going to learn how to remove an element from a dictionary? Also, writing a Python program to remove elements from a dictionary.
Submitted by Shivang Yadav, on March 22, 2021

There are some of the ways to remove the elements from the dictionary,

  1. Using the del keyword
  2. Using the pop() method
  3. Using the popitem() method
  4. Using the clear() method

1) Remove elements from a dictionary using the del keyword

The del keyword can be used to delete an item from the dictionary and whole dictionary as well.

  • To delete an item – Provide the key name
    Syntax:
    del dictionary_name[key_name]
    
  • To delete whole dictionary – There is no need to provide the key name
    Syntax:
    del dictionary_name
    
    Note: After deleting the dictionary, if we print it or use it in any way – it will raise an error.

Program:

# Python program to remove elements from a dictionary 
# using the del keyword 

# Dictionary 
Record = {'id' : 101, 'name' : 'Amit Kumar', 'age' : 21} 

# printing the Dictionary
print("Record...")
print(Record)

# Removing elements
del Record['id']
del Record['age']

# printing the Dictionary
print("Record after deleting the elements...")
print(Record)

Output:

Record...
{'id': 101, 'age': 21, 'name': 'Amit Kumar'}
Record after deleting the elements...
{'name': 'Amit Kumar'}
# Python program to remove the dictionary 
# using the del keyword 

# Dictionary 
Record = {'id' : 101, 'name' : 'Amit Kumar', 'age' : 21} 

# printing the Dictionary
print("Record...")
print(Record)

del Record

# printing the Dictionary
print("Record after deleting...")
print(Record)

Output:

Record...
{'id': 101, 'name': 'Amit Kumar', 'age': 21}
Record after deleting...
Traceback (most recent call last):
  File "main.py", line 15, in <module>
    print(Record)
NameError: name 'Record' is not defined

2) Remove elements from a dictionary using the pop() method

The pop() method is used to delete a specified element from the dictionary and also returns the element (value) to be deleted.

Syntax:

dictionary_name.pop(key_name)

Program:

# Python program to remove elements from a dictionary 
# using the pop() method 

# Dictionary 
Record = {'id' : 101, 'name' : 'Amit Kumar', 'age' : 21} 

# printing the Dictionary
print("Record...")
print(Record)

# deleting the elements
ele = Record.pop('id')
print('Deleted value is:', ele)

ele = Record.pop('age')
print('Deleted value is:', ele)

# printing the Dictionary
print("Record after deleting the elements...")
print(Record)

Output:

Record...
{'age': 21, 'name': 'Amit Kumar', 'id': 101}
Deleted value is: 101
Deleted value is: 21
Record after deleting the elements...
{'name': 'Amit Kumar'}

3) Remove elements from a dictionary using the popitem() method

The popitem() method is used to return and removes an arbitrary element (key, value) pair from the dictionary.

Syntax:

dictionary_name.popitem()

Program:

# Python program to remove elements from a dictionary 
# using the popitem() method 

# Dictionary 
Record = {'id' : 101, 'name' : 'Amit Kumar', 'age' : 21} 

# printing the Dictionary
print("Record...")
print(Record)

# deleting the elements
ele = Record.popitem()
print('Deleted value is:', ele)

ele = Record.popitem()
print('Deleted value is:', ele)

# printing the Dictionary
print("Record after deleting the elements...")
print(Record)

Output:

Record...
{'id': 101, 'age': 21, 'name': 'Amit Kumar'}
Deleted value is: ('id', 101)
Deleted value is: ('age', 21)
Record after deleting the elements...
{'name': 'Amit Kumar'}

4) Remove elements from a dictionary using the clear() method

The clear() method clears the dictionary i.e., removes all elements from the dictionary.

Note: The del keyword deletes the dictionary but the clear() method removes the elements only and does not remove the dictionary.

Syntax:

dictionary_name.clear()

Program:

# Python program to remove all elements from a dictionary 
# using the clear() method 

# Dictionary 
Record = {'id' : 101, 'name' : 'Amit Kumar', 'age' : 21} 

# printing the Dictionary
print("Record...")
print(Record)

# clearing the elements
Record.clear()

# printing the Dictionary
print("Record after clearing all elements...")
print(Record)

Output:

Record...
{'age': 21, 'id': 101, 'name': 'Amit Kumar'}
Record after clearing all elements...
{}

Python Dictionary Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.