How to delete a set using the "del" keyword in Python

Overview

Python’s del keyword deletes a set completely.

Syntax

del set

Parameter value

The del keyword does not take a parameter value.

Code example

# creating a set
myset = {"apple", "banana", "cherry"}
# deleting the set using the del keyword
del myset
print(myset) # this will raise an error because the set we created no longer exists

Code explanation

The code above returns an error because myset, the set we tried printing in line 7, no longer exists. This is because we deleted it using the del keyword in line 5.

New on Educative
Learn any Language for FREE all September 🎉
For the entire month of September, get unlimited access to our entire catalog of beginner coding resources.
🎁 G i v e a w a y
30 Days of Code
Complete Educative’s daily coding challenge every day in September, and win exciting Prizes.

Free Resources