Wednesday, January 15, 2025
HomeTechHow can I completely delete a list in Python?

How can I completely delete a list in Python?

To completely delete a list in Python, you can use the del statement. This removes the list from memory entirely.

Syntax:

python
del list_name

Example:

python
my_list = [1, 2, 3, 4, 5]
print(my_list) # Output: [1, 2, 3, 4, 5]

del my_list

# Trying to access my_list after deletion will result in a NameError
# print(my_list) # Uncommenting this will raise a NameError

Notes:

  • After using del, the list will no longer exist, and you cannot use it further.
  • Ensure you don’t try to access the list after deletion, as it will lead to a NameError.
RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x