How to remove all the elements of a dictionary in Python?


A dictionary is a Python is a container that maintains mappings of unique keys to values in an unordered and mutable manner. Data values are stored in key:value pairs using dictionaries. Dictionaries are written with curly brackets and have keys and values.

Dictionaries are now ordered as of Python 3.7. Dictionaries in Python 3.6 and before are not sorted.

Example

This example shows the working of a dictionary in python.

thisdict = { "companyname": "Tutorialspoint", "tagline" : "simplyeasylearning", } print(thisdict)

Output

The above code produces the following results

{'companyname': 'Tutorialspoint', 'tagline': 'simplyeasylearning'}

In this article, we remove all the elements of a dictionary in python using different methods.

Using the clear() method

By using the clear method, we remove all the elements of the dictionary. The clear() method does not take any argument and does not return any value.

Example

Here is the following example code, to remove all the elements of a dictionary using the clear() method.

this_dict = { "companyname": "Tutorialspoint", "tagline" : "simplyeasylearning", } print("Dictonary before removing elements:") print(this_dict) this_dict.clear() print("Dictionary after removing elements from it") print(this_dict)

Output

Dictonary before removing elements:
{'companyname': 'Tutorialspoint', 'tagline': 'simplyeasylearning'}
Dictionary after removing elements from it
{}

By reassigning to an empty dictionary

This is the easiest way to remove all the elements of a dictionary. Here we reassign the dictionary to an empty dictionary and in doing so, all the elements of the dictionary will be lost. The following example shows the working of this method.

this_dict = { "companyname": "Tutorialspoint", "tagline" : "simplyeasylearning", } print("Dictonary before removing elements:") print(this_dict) this_dict={} print("Dictionary after removing elements from it") print(this_dict)

Output

The output produced is as follows.

Dictonary before removing elements:
{'companyname': 'Tutorialspoint', 'tagline': 'simplyeasylearning'}
Dictionary after removing elements from it
{}

Using del and a loop

In this method, we iterate each element of a dictionary using a for loop. And each of these elements is removed from the dictionary using the del operator.

Example 1

In this example, we will see how to remove all the elements of a dictionary using the del method and a loop.

this_dict = { "companyname" : "Tutorialspoint", "tagline" : "simplyeasylearning"} print("Dictonary before removing elements:") print(this_dict) keys = list(this_dict.keys()) for ele in keys: del this_dict[ele] print("Dictionary after removing elements from it") print(this_dict)

Output

The output produced is as follows.

Dictonary before removing elements:
{'companyname': 'Tutorialspoint', 'tagline': 'simplyeasylearning'}
Dictionary after removing elements from it
{}

Example 2

Following is another example of emptying a dictionary using the del operator −

my_dict = {'name': 'foo', 'age': 28} keys = list(my_dict.keys()) for key in keys: del my_dict[key] print(my_dict)

Output

This will give the output −

{}

Updated on: 05-Sep-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements