๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ del
Python del Statement (With Examples)
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] # deleting the third item del my_list[2] # Output: [1, 2, 4, 5, 6, 7, 8, 9] print(my_list) # deleting items from 2nd to 4th del my_list[1:4] # Output: [1, 6, 7, 8, 9] print(my_list) # deleting all elements del my_list[:] # Output: [] print(my_list) person = { 'name': 'Sam', 'age': 25, 'profession': 'Programmer' } del person['profession'] # Output: {'name': 'Sam', 'age': 25} print(person) ... Note: You can't delete items of tuples and strings in Python.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_keyword_del.asp
Python del Keyword
In Python everything is an object, so the del keyword can also be used to delete variables, lists, or parts of a list etc.
๐ŸŒ
Real Python
realpython.com โ€บ python-del-statement
Python's del: Remove References From Scopes and Containers โ€“ Real Python
October 21, 2023 - Hereโ€™s the general syntax of the del statement in Python: ... The del statement allows you to remove one or more references from a given namespace. It also lets you delete data from mutable container types, such as lists and dictionaries.
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ python-list-del-function
Python List del Function
March 26, 2025 - The list del function removes the value at a specified index. After deleting, the remaining values moved up to fill the index gap. If we know the Index value or want to eliminate a range of items from the given object, then use the del statement ...
๐ŸŒ
Dot Net Perls
dotnetperls.com โ€บ del-python
Python - del Operator - Dot Net Perls
Here we call del to remove the third element in a list. We compare this to the remove() method on list, which searches for the first matching value and then deletes it. Tip To use del on a list we must specify an index or a slice.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ How-does-del-operator-work-on-list-in-Python
How does del operator work on list in Python?
April 4, 2023 - The del operator removes an item or an element from the list at the provided index location, but the item is not returned. In short, this operator accepts as an input the item's index to be removed and deletes the item at that index.
๐ŸŒ
Real Python
realpython.com โ€บ ref โ€บ keywords โ€บ del
del | Python Keywords โ€“ Real Python
In Python, the del keyword deletes names from a given scope or namespace. This includes variables, list items, and dictionary keys, among other objects. It can help manage memory by removing references to objects, allowing them to be ...
Top answer
1 of 2
2

The answer is no, it shouldn't. It's intended to delete all of the elements of the list. Looking at the documentation, the result of s.clear() (where s is a mutable sequence type, for example a list) is:

removes all items from s (same as del s[:])

Hence, del s[:] is the same as s.clear() in that it removes all items from s.

Perhaps, this is a bit more understandable if you consider that the function called behind the scenes is __delitem__. From the docs:

Called to implement deletion of self[key]. Same note as for __getitem__(). This should only be implemented for mappings if the objects support removal of keys, or for sequences if elements can be removed from the sequence. The same exceptions should be raised for improper key values as for the __getitem__() method.

Consider the following difference:

a = [1,2,3]
b = a

del a
# print(a) ## raises an error
print(b)   ## prints [1,2,3]

c = [1,2,3]
d = c

del c[:]
print(c)   ## prints []
print(d)   ## prints []

So why would you want del a[:] to behave this way? Well, think of it as just a special case of deleting a slice of a list. For example, say that you'd want to delete the 3rd, 4th, and 5th element of a long list a = list(range(40)). With the slice notation and the __delitem__ this is easy, just use del a[3:6]. Now try to do the same with a for loop and you'll soon find out it can get quite cumbersome. Heck, just try to delete all the items of a (but not the a itself!) with a for loop ;)

2 of 2
0

Because a[:] is is simply just a copy of a, consisting of the identical objects as a but a different object than a. Their elements are identical but they are not.

Let's create a list and do some id checks:

a = [1, 2, 3]
print(id(a)) # 97731118088
print(id(a[:])) # 97731213576
print(id(a[:])) # 97731212104
print(id(a[:])) # 97731198600

Notice the changing ids of the copy a[:]. It is an object that is created on the fly every time it is called, and most importantly, even a[:] is a[:] not True! Let's look at the ids of their elements to come to a conclusion:

print(id(a[0])) # 1648192992
print(id(a[:][0])) # 1648192992
a[0] is a[:][0] # True
a[1] is a[:][1] # True
a[2] is a[:][2] # True

So, we can conclude that a[:] is an object that consists of the very elements of a but is a different object than a, and is a different object every time it is called. The elements of a and a[:] are all identical, but they themselves are not.

So what del a[:] does is to remove all of the elements of a. That way a is mutated, and you end up with an empty a, i.e. []. However, del a removes the name a from namespace completely, and when you ask Python to print a for you, you'll get a NameError: name 'a' is not defined.

But how do we know that? Well, let's gain some perspective by disassembling del on a vs a[:]:

Let's define two functions:

def deletion1(a):
    del a

def deletion2(a):
    del a[:]

Let's disassemble them:

import dis

dis.dis(x = deletion1)

  1           0 DELETE_FAST              0 (a)
              2 LOAD_CONST               0 (None)
              4 RETURN_VALUE

dis.dis(x = deletion2)
  1           0 LOAD_FAST                0 (a)
              2 LOAD_CONST               0 (None)
              4 LOAD_CONST               0 (None)
              6 BUILD_SLICE              2
              8 DELETE_SUBSCR
             10 LOAD_CONST               0 (None)
             12 RETURN_VALUE

The dis documentation indicates that the DELETE_FAST operation, which the first function does, simply "Deletes local co_varnames[var_num]". This is basically removal of that name a so that you can't reach the list object anymore. Beware, this does not remove the object that is referred to by the name a, but just removes its name so that the a is not a reference to anything anymore. The object 97731118088 is still the same list, [1, 2, 3]:

import gc
for obj in gc.get_objects():
    if id(obj) == 97731118088:
        print(obj)
        # [1, 2, 3]

On the other hand, again from the documentation, DELETE_SUBSCR "Implements del TOS1[TOS]", which is basically "an in-place operator that removes the top of the stack and pushes the result back on the stack". This way, the stack elements are removed, and you are left with the name a, which now refers to a list whose elements are deleted, i.e. just an "empty shell", if you will. After this operation, a becomes [], but has still the same id of 97731118088. Just its elements are gone via an in-place deletion.

๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ how to use python del statement?
How to Use Python del Statement? | Scaler Topics
May 4, 2023 - Note: The del keyword can be used to delete an item from a list by referring to its index rather than its value. Example: In this example, we have created a class that outputs the value of a variable.
Find elsewhere
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Remove an Item from a List in Python: remove, pop, clear, del | note.nkmk.me
April 17, 2025 - In Python, you can remove items (elements) from a list using methods such as remove(), pop(), and clear(). You can also use the del statement to delete items by index or slice. Additionally, list comp ...
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ removing items from python lists: `del` vs `pop` vs `remove`
Removing items from Python lists: `del` vs `pop` vs `remove` | Sentry
2 weeks ago - The del method is useful when deleting an item at a specific position in the list. For example: mylist = ['a', 'b', 'c', 'd'] del mylist[2] # will delete the item at index 2, that is 'c' print(mylist) # will print ['a', 'b', 'd']
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-del-to-delete-objects
Python del keyword - GeeksforGeeks
July 12, 2025 - Since everything in Python is an object, del helps remove references to these objects and can free up memory ยท del Keyword removes the reference to an object. If that object has no other references, it gets cleared from memory.
๐ŸŒ
Sivo
blog.sivo.it.com โ€บ home โ€บ python list manipulation โ€บ how to use del in python list?
How to use del in Python list? | Python List Manipulation โ€“ Sivo
August 25, 2025 - Original list: ['apple', 'banana', ... for removing multiple elements based on their positions. To delete a slice of items, use the syntax del list_name[start:end]. Remember that end is exclusive, meaning the item at the end index itself will not be deleted....
๐ŸŒ
Python Pool
pythonpool.com โ€บ home โ€บ blog โ€บ python del keyword [with examples]
Python del Keyword [With Examples] - Python Pool
June 14, 2021 - Python del statement doesnโ€™t return anything. The del statement can be used to remove an item from a list by referring to its index, rather than its value.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ datastructures.html
5. Data Structures โ€” Python 3.14.6 documentation
This differs from the pop() method which returns a value. The del statement can also be used to remove slices from a list or clear the entire list (which we did earlier by assignment of an empty list to the slice).
๐ŸŒ
PyTutorial
pytutorial.com โ€บ python-list-del-remove-items-easily
PyTutorial | Python List del: Remove Items Easily
May 30, 2026 - Remember the syntax: del list[index]. Practice with examples. Avoid common mistakes like index errors. Use del when you need to delete without returning a value. For other cases, explore pop() or remove(). Now you know how to use Python list del.
๐ŸŒ
Toppr
toppr.com โ€บ guides โ€บ python โ€บ methods-and-functions โ€บ del โ€บ python-del-statement-with-examples
Python del Statement (with Examples) | What is Python del? | Definition
June 29, 2021 - To delete an item or a data value from the list, set, dictionary, or any other mutable iterables in Python. Attributes of the class instances can be deleted. To remove a slicing from a list of iterables. In the below program, we have created a class that prints the value of a variable.
๐ŸŒ
Guru99
guru99.com โ€บ home โ€บ python โ€บ remove element from a python list [clear, pop, remove, del]
Remove element from a Python LIST [clear, pop, remove, del]
August 12, 2024 - Let us see the working of clear() in the example below: my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya'] #Using clear() method element = my_list.clear() print(element) print(my_list) ... To remove an element from the list, you can use the del keyword followed by a list.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ list-methods-in-python-set-2-del-remove-sort-insert-pop-extend
List Methods in Python | Set 2 (del, remove(), sort(), insert(), pop(), extend()...) - GeeksforGeeks
# Python code to demonstrate the working of # del and pop() # initializing list lis = [2, 1, 3, 5, 4, 3, 8] # using del to delete elements from pos.
Published ย  April 7, 2025
๐ŸŒ
Toppr
toppr.com โ€บ guides โ€บ python-guide โ€บ questions โ€บ what-is-the-use-of-del-in-python
What is the Use of Del in Python? | Python Questions
August 5, 2021 - Pythonโ€™s del statement is used to delete variables and objects in the Python program. Iterable objects such as user-defined objects, lists, set, tuple, dictionary, variables defined by the user, etc. can be deleted from existence and from the memory locations in Python using the del statement.