Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - REMOVE FROM DICTIONARY


How to remove an element from the Dictionary?


Let us say, we have a Dictionary that contains,



  5, Is a Number
  John, Is a Name
  Python, Is a Language

As 'Key' and 'Value' pair.


Now, if you want to remove the entries from a 'Dictionary', 'pop( )', 'popitem( )', 'clear( )' Function and 'del' keyword can be used.


Let us look at the 'pop( )' Function first.


How to remove an element using the pop( ) Function?


The 'pop( )' Function can be used to remove an item from the Dictionary.


Let us say, we want to remove the entry where the 'Key' is '5' and 'Value' is 'Is a Number'.


5, Is a Number

Example :


x = {
    5 : "Is a Number", 
    "John": "Is a Name", 
    "Python": "Is a Language"
}
x.pop(5)
print(x)


Output :



  {'John': 'Is a Name', 'Python': 'Is a Language'}

So, in the above code we have created a 'Dictionary' using braces '{ }' and 'Key' and 'Value' pairs.


x = {
	5 : "Is a Number", 
    "John": "Is a Name", 
    "Python": "Is a Language"
}

And initialised to the variable 'x'.


java_Collections

Now, we are supposed to delete the value of the 'Key', '5'.


So, we have used the below way to update it.


x.pop(5)

And the entry for the 'Key' '5' is deleted for the 'Dictionary'.


java_Collections

And we get the below output,


Output :



  {'John': 'Is a Name', 'Python': 'Is a Language'}

How to remove an element using the popitem( ) Function?


The 'popitem( )' Function is used to remove the last inserted element from the Dictionary.


Note : Before version 3.7 'popitem( )' Function used to remove any random element from the 'Dictionary'.

Example :


x = {
    5 : "Is a Number", 
    "John": "Is a Name", 
    "Python": "Is a Language"
}
x.popitem()
print(x)


Output :



  {5: 'Is a Number', 'John': 'Is a Name'}

So, in the above code we have created a 'Dictionary' using braces '{ }' and 'Key' and 'Value' pairs.


x = {
    5 : "Is a Number", 
    "John": "Is a Name", 
    "Python": "Is a Language"
}

And initialised to the variable 'x'.


java_Collections

Now, we are supposed to delete the last inserted element. And the last inserted element is,


java_Collections

So, we have used the 'popitem( )' to delete it.


x.popitem( )

And the last entry is deleted for the 'Dictionary'.


java_Collections

And we get the below output,


Output :



  {5: 'Is a Number', 'John': 'Is a Name'}

How to remove all the elements from the Dictionary using clear( ) Function?


The 'clear( )' Function can be used to remove all the elements from the Dictionary.


Example :


x = {
    5 : "Is a Number", 
    "John": "Is a Name", 
    "Python": "Is a Language"
}
x.clear()
print(x)


Output :



  { }

So, in the above code we have created a 'Dictionary' using braces '{ }' and 'Key' and 'Value' pairs.


x = {
    5 : "Is a Number", 
    "John": "Is a Name", 
    "Python": "Is a Language"
}

And initialised to the variable 'x'.


java_Collections

And we have used the 'clear( )' method to remove all the elements from the 'Dictionary'.


x.clear( )

And the print statement, prints an empty 'Dictionary'.


Output :



  { }

How to delete a Dictionary?


We have seen, the 'clear( )' Function can be used to remove all the elements from the Dictionary.


Now, let us see, how can we delete a Dictionary completely.


The 'del' keyword is used to remove the Dictionary completely.


Example :


x = {
    5 : "Is a Number", 
    "John": "Is a Name", 
    "Python": "Is a Language"
}
del x
print(x)


Output :



  print(x)
 NameError: name 'x' is not defined

So, we have used the 'del' keyword to delete the Dictionary completely.


del x

And when we try printing the Dictionary,


print(x)

It ends up with an error.


 print(x)
NameError: name 'x' is not defined

As the Dictionary 'x' no longer exist.