How to Delete Multiple Keys Items From Dictionary In Python

In this tutorial, learn how to delete multiple keys items from Dictionary using Python. The short answer is: use Python pop() function to delete single or multiple keys items of Dictionary. You can also delete all items in Dictionary by using the Python clear() function.

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.

How to delete multiple elements of Dictionary in Python

How to Delete Single Item From Dictionary Using Python pop()

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 single quotes(‘). Hence, the output gives the elements without the element with the key ‘two’.

Bonus: download a Free Python cheat sheet that will show you 20+ most important examples to learn in Python.

Delete Multiple Keys Items of Dictionary Using pop() in 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 for the mentioned list of keys from dictionary in Python. 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 the key ‘two’. The example delete list of keys from dictionary which are ‘one’, ‘three’, and ‘four’.

The most noteworthy thing is, you are using the same pop() to delete single/multiple elements. To delete single, you have to use a single key. For multiple elements, you have to use multiple comma-separated list of keys from dictionary.

How to Remove All Items From Dictionary Using Python Clear()

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.

DOWNLOAD Free PYTHON CHEAT SHEET

Related Posts