Q:

Python program for removing elements from a dictionary

belongs to collection: Python Dictionary Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Dictionary Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to change the dictionary items... >>
<< Python program for accessing elements from a neste...