Python dictionary remove

In this Python tutorial, we will discuss Python dictionary remove. Here we will also cover the below examples:

  • Python dictionary remove key
  • Python dictionary remove duplicate keys
  • Python dictionary remove element
  • Python dictionary remove last key
  • Python dictionary remove key-value pair
  • Python dictionary remove empty values
  • Python dictionary remove key based on value
  • Python dictionary remove none values
  • Python dictionary remove duplicate values
  • Python dictionary remove value

Python dictionary remove

  • Let us see how to remove a dictionary in Python
  • In Python, the clear() method is used to delete a dictionary Python.
  • This method removes all the keys values pairs that are available in the dictionary and always returns a none value or empty dictionary.

Syntax:

Here is the Syntax of the clear() method

dict.clear()

Example:

Let’s take an example and check how to remove a dictionary in Python

my_dict = {'john':3,'George':6,'Smith':9,'Brown':6}
b = dict.clear(my_dict)
print("Remove dictionary:",b)

Here is the example of the following given code

Python  dictionary remove
Python dictionary remove

Read: Python dictionary length

Python dictionary remove key

  • Here we can see how to remove a key from dictionary.
  • To remove a key in a dictionary there are various method to perform this task.
  • The following ways to remove a key from dictionary are
    • By using pop() method
    • By using del keyword
    • By using dict comprehension and item() method

Examples:

Let’s take an example and check how to remove a key from a dictionary by using the pop() method

In Python, the pop() method can be used to remove a key and the value from a dictionary. This method can be used to remove an item from a particular index value. The pop() method takes only one parameter that is the name of the key which we want to remove from a dictionary.

Code:

my_dict = {'Australia':250,'Germany':650,'Japan':900,'England':500}
rem_key = my_dict.pop('Germany')

print("Remove Key from dictionary:",rem_key)
print(my_dict)

Here is the Screenshot of the following given code

Python dictionary remove key
Python dictionary remove key

Read: Python Dictionary index

By using del keyword to remove a key from dictionary

  • In python del keyword can be used to delete an object or item that is available in the dictionary.
  • This method raises a Key-error if key is not available in a dictionary.

Code:

my_dict = {'U.S.A':450,'China':320,'Japan':680,'Polland':500}
del my_dict['Japan']


print("Remove a key from dictionary:",my_dict)

Here is the execution of the following given code

Python dictionary remove key from del
Python dictionary remove the key from del

Read: Python dictionary initialize

By using dict comprehension and item() method

  • In python, the items() and dict comprehension can also help the user to remove a key from a dictionary.
  • The items() method takes no parameters and always returns an items to store in a list.
  • Dict comprehension is basically used for creating a dictionary by including all the key-value pairs.

Let’s take an example and check how to remove a key from the dictionary by using dict comprehension and items() method

to_dict = {"Mangoes" : 40, "Grapes" :29, "Oranges" : 39, "Apple" : 49}
  

new_key = {key:val for key, val in to_dict.items() if key != 'Grapes'}

print ("Remove after key is :",(new_key))

Here is the Screenshot of the following given code

Python dictionary remove key from dict comprehension
Python dictionary remove the key from dict comprehension

Python dictionary remove dupliacte keys

  • Here we can see how to remove duplicate keys from a dictionary in Python.
  • By using the loop and items() method we can easily remove a duplicate item from a dictionary.
  • In this example first, we will initialize a dictionary and remove duplicate keys in a dictionary using loops, this method will remove any duplicate values from our dictionary.

Example:

Let’s take an example and check how to remove duplicate keys from a dictionary

my_dictionary = { 'John' : 20, 'George' : 45, 'james' : 10, 'George' : 60, 'potter' : 70}
  
new_key = []
output = dict()
for new_k, new_val in my_dictionary.items():
    if new_val not in new_key:
        new_key.append(new_val)
        output[new_k] = new_val
  
print("Remove duplicate key is:",output)

Here is the Screenshot of the following given code

Python dictionary remove duplicate keys
Python dictionary remove duplicate keys

This is how to remove duplicate keys from a dictionary

Also Read: Python dictionary filter

Python dictionary remove element

  • Let us see how to remove an element from a dictionary.
  • By using dict.keys method and del keyword we can easily remove an element from a dictionary.
  • The dict.keys() method always returns an item that displays a list of the keys of the dictionary. Now create a for loop to iterate the items from the dictionary.
  • In this example create a variable “selected_key” and assign them a key element which we have to remove from a dictionary. Use the del keyword and check the indexing value and then call the break statement.

Example:

Let’s take an example and check how to remove an element from a dictionary

my_dict = {"Italian": 9, "Chinese" : 12, "Mexican": 7}

select_key = "Chinese"
new_key ="Italian"
for n_key in my_dict.keys():
  if n_key == select_key:
    del my_dict[n_key]
    break
for n_key in my_dict.keys():
  if n_key == new_key:
    del my_dict[n_key]
    break
print("Remove element from dictionary",my_dict)

Here is the Screenshot of the following given code

Python dictionary remove element
Python dictionary remove element

This is how to remove an element from a dictionary

Another example to check how to delete an element from a dictionary

In this example, we can easily use the method pop() to remove an element from a dictionary. First, we will create an employee list and store two dictionaries. Each dictionary contains three keys. Now we have to remove an element from a dictionary by using the pop() method.

Example:

employee_info = { "emp_id": 234, "emp_name": "Adam", "emp_address": "England" }

new_key = employee_info.pop("emp_address")
print(new_key)
print("Remove element from dictionary",employee_info)

Here is the Screenshot of the following given code

Python dictionary remove element pop
Python dictionary remove element pop

Read: Python dictionary values to list

Python dictionary remove last key

  • Let us see how to remove the last key from a dictionary in Python.
  • In this example we can easily use items() and for loop method to remove the last key from the dictionary.

Example:

Let’s take an example and check how to remove the last key from the dictionary

my_dict = {"Steve": 234,"Mark": 134,"Robert": 278,"Chris": 43}
remove_key = 'Chris' # last key
to_dictionary = {}
for new_key, new_value in my_dict.items():
    if new_key is not remove_key:
        to_dictionary[new_key] = new_value
my_dict = to_dictionary
print(" Remove last key from dictionary:",my_dict)

Here is the Screenshot of the following given code

Python dictionary remove last key
Python dictionary remove the last key

Another example to check how to remove a last key from the dictionary

  • In this example, we can easily use the pop.item() method to remove a last key from the dictionary.

Example:

new_dictionary = {"Andrew": 120,"Hayden": 140,"Mathew": 210,"Gilchrisht": 174}
rem_key = new_dictionary.popitem()

print("remove last key:",rem_key) # remove last key
print(new_dictionary)

Here is the Screenshot of the following given code

Python dictionary remove last key
Python dictionary remove the last key

Read: Python Dictionary update 

Python dictionary remove key-value pair

  • Here we can see how to remove the key-value pair from a dictionary
  • By using del keyword we can remove the key-value pair from a dictionary.

Example:

Let’s take an example and check how to remove the key-value pair from a dictionary

my_dict = {"a": 120,"b": 30,"c": 150,"d": 143}
del my_dict["b"]

print("remove key-value pair:",my_dict)

Here is the Screenshot of the following given code

Python dictionary remove key value pair
Python dictionary remove key-value pair

Another example to check how to remove the key-value pairs from the dictionary

  • By using the popitem() method we can easily remove the key-value pairs from the dictionary.
  • In python popitem() method does not take any argument. This method is used to remove a random element from a dictionary and always returns its value.

Syntax:

Here is the Syntax of popitem() method

dict.popitem()

Example:

my_dict = {"m": 60,"n": 40,"o": 10,"p": 14}
new_item = my_dict.popitem()

print("remove key-value pair:",new_item)
print(my_dict)

Here is the Screenshot of the following given code

Python dictionary remove key value pair
Python dictionary remove key-value pair

This is how to remove key-value pairs from the dictionary

Read: Python Concatenate Dictionary

Python dictionary remove empty values

  • Let us see how to remove empty values from a dictionary.
  • By using list comprehension method we can easily perform this task.

Example:

to_dict = [{"u": 10,"v": 50,"p": '',"z":''}]
output = [element for element in ({new_key: new_val for new_key, new_val in sub.items() if new_val} for sub in to_dict) if element]
                                         

print("Remove empty values: ",output)

In the above example first, we will initialize a list and remove empty values by using list comprehension.

Here is the Screenshot of the following given code

Python dictionary remove empty values
Python dictionary remove empty values

Python dictionary remove key based on value

  • Here we can see how to remove a value based on key from a dictionary.
  • By using dictionary comprehension and items() method we can easily remove key based on value.

Example:

my_dict = {"u" : 12, "d" :16, "e" : 19, "f" : 15}
  

rem_key = {n_key:n_val for n_key, n_val in my_dict.items() if n_val != 19} # selected value

print ("Remove after key based on value is :",(rem_key))

Here is the Screenshot of the following given code

Python dictionary remove key based on value
Python dictionary remove key based on the value

This is how to remove a key based on the value

Also Read: Python dictionary append

Python dictionary remove none values

  • Let us see how to remove none values from dictionary.
  • By using for loop and list comprehension method we can easily remove none values from dictionary.
  • In this example first we will initialize a list and use list comprehension method to check if the key-value pair is none or not in a dictionary

Example:

Let’s take an example and check how to remove none values from a dictionary

to_dict = {'t': 40, 's': 30,'d':None,'n':None}

output = []
for value in to_dict:
    if to_dict[value] is not None :
        output.append(value)
  
print(output)

Here is the Screenshot of the following given code

Python dictionary remove none values
Python dictionary remove none values

Python dictionary remove duplicate values

  • To remove duplicate values from a dictionary we can easily use dictionary comprehension method.

Example:

Let’s take an example and check how to remove duplicate values from a dictionary

my_dictionary = {'Australia': 20, 'Switzerland': 30,'Newzeland':30,'Moscow':70}


new_element = {new_val : new_key for new_key, new_val in my_dictionary.items()}
new_output = {new_val : new_key for new_key, new_val in new_element.items()}
print(new_output)

Here is the Screenshot of the following given code

Python dictionary remove duplicate values
Python dictionary remove duplicate values

Python dictionary remove value

  • Let us see how to remove a value from the dictionary.
  • By using the dict comprehension and items() method we can easily remove value from the dictionary.
  • The items() method always returns an object of the dictionary. The object is a collection of key-value tuples. The items() method does not take any arguments.
  • Dict comprehension is basically used for creating a dictionary by including all the key-value pairs.

Example:

Let’s take an example and check how to remove a value from the dictionary

dictionary = {"a" : 40, "z" :29, "c" : 39, "v" : 49}
  

rem_value = {new_key:new_val for new_key, new_val in dictionary.items() if new_val != 49}

print ("Remove after value is :",(rem_value))

Here is the Screenshot of the following given code

Python dictionary remove value
Python dictionary remove the value

This is how to remove a value from the dictionary in Python

You may also like reading the following.

In this Python tutorial, we will discuss Python dictionary remove. Here we will also cover the below examples:

  • Python dictionary remove key
  • Python dictionary remove duplicate keys
  • Python dictionary remove element
  • Python dictionary remove last key
  • Python dictionary remove key-value pair
  • Python dictionary remove empty values
  • Python dictionary remove key based on value
  • Python dictionary remove duplicate values
  • Python dictionary remove none values
  • Python dictionary remove value