How to Delete Multiple Keys Items From Dictionary In Python

In this tutorial, learn how to delete multiple items of Dictionary using Python. The short answer is: use the pop() function of Python to remove single or multiple elements.

First of all, if you have so many elements with few elements are not required to be in Dictionary. You can remove one, two or more other elements using the keys as given in the below examples. You may also like to read how to update the multiple items to Dictionary using Python.

How to delete multiple elements of Dictionary in Python

Remove Single Item From Dictionary in Python

To remove the single element of the Dictionary, you have to use the pop() function of Python. In the function, you have to pass the single key which you want to remove.

If the key is a string, enclose it within the quotes(‘). While, if the integer is key to delete, you have to just pass it without any quotes.

Output

{‘one’: ‘Sally’, ‘three’: ‘Dingra’, ‘four’: ‘Lilop’}

The above example contains 4 elements before the code execution. After the execution of the code, the Dictionary showing only the 3 elements.

Because the key is a string, you have to pass it as an argument and within quotes(‘). Hence, the output gives the elements without the element with key ‘two’.

Delete Multiple Keys Items of Dictionary Using Python

If you want to delete furthermore the number of elements of the Dictionary. You have to use the pop() function and pass the multiple numbers of keys as an argument. Add the keys as the argument of the pop() function and with comma(,) separation.

You have to put the keys in the second variable as given in the example below. Now, use the for loop of Python to iterate through each key of the new variable. In each iteration of the loop, the elements get deleted. Hence, you will get the final variable without the removed elements.

Output

{‘two’: 13}

The above example gives the output after the for loop iteration. As a result, there is only one element remain after the deletion which is with key ‘two’.

The most noteworthy thing is, you are using the same pop() to delete single/multiple elements.

Use Clear() Function To Delete All Items From Dictionary

Likewise, the above all examples remove single or multiple elements in Python. But, Probably, sometimes you maybe want to clear all the elements from the Dictionary. For the reason that you have to use another method called clear() function.

the clear() function requires no argument to remove all the elements. Check the below example to perform this task.

Output

{}

Finally, the above output shows that there are no elements in myDict. You may see only the curly({}) brackets in the output.

We have covered almost all the available methods here to remove or clear elements.

You may also like to read

Hope, you like this post of how to delete multiple elements from myDict. If you have any query regarding the tutorial, please comment below.

Reference
Stackoverflow Discussion on Removing Multiple Keys of Dictionary in Python

Also tell me, which method you are using to clean your variable in Python by commenting below.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.