Python’s del statement will allow you to remove names and references from different namespaces. It’ll also allow you to delete unneeded items from your lists and keys from your dictionaries. If you want to learn how to use del in your Python code, then this tutorial is for you. The del statement empowers you to better manage memory resources in your code, making your programs more efficient.
In this tutorial, you’ll learn how to:
- Write and use
delstatements in Python - Take advantage of
delto remove names from different scopes - Use
delfor removing list items and dictionary keys - Remove object attributes using
del - Write classes that prevent attribute deletion using
del
To get the most out of this tutorial, you should be familiar with Python’s built-in data types, specifically lists and dictionaries. You should also know the basics of classes and object-oriented programming in Python.
Free Bonus: Click here to download sample code that’ll empower you to write more efficient programs with Python’s del statement.
Getting to Know Python’s del Statement
Python’s del statement allows you to remove references to objects from a given namespace or container type. It performs an operation that’s the opposite of what an assignment statement does. It’s sort of an unassignment statement that allows you to unbind one or more names from their referenced objects.
This is a natural feature to have in Python. If you can create a variable by writing variable = value, then you must have the option to undo this operation by deleting variable. That’s where del comes on the scene.
The del statement can come in handy in different coding situations, such as:
- Freeing up memory resources
- Preventing accidental use of variables and names
- Avoiding naming conflicts
Of course, this list is incomplete. You may find some other appropriate use cases for this statement. In this tutorial, you’ll learn how the del statement works and how to use it in your code. To kick things off, you’ll start by learning the general syntax of del in Python.
Learning the del Syntax
A Python del statement consists of the del keyword, followed by a comma-separated series of references.
Note: In this tutorial, you’ll use the term reference to generically designate names or identifiers that can hold references to objects in Python.
Here’s the general syntax of the del statement in Python:
del reference_1[, reference_2, ..., reference_n]
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.
You’ll often use this statement with a single argument. However, it also supports a series of arguments separated by commas.
In the above construct, reference_* represents any kind of identifier that can hold references to concrete objects stored in memory. In practice, these references can include:
- Identifiers, such as variables and names of functions, classes, modules, and packages
- Indices of mutable sequences, such as
a_list[index] - Slices of mutable sequences, like
a_list[start:stop:step] - Keys of dictionaries, like
a_dict[key] - Members of classes and objects, such as attributes and methods
You can use any of these references as arguments to del. If you use a comma-separated series of arguments, then keep in mind that del operates on every argument sequentially from left to right. This behavior can be risky when you’re removing items from lists, as you’ll learn later in this tutorial.
Here’s a quick example of using del to remove a variable:
>>> greeting = "Hi, Pythonista!"
>>> greeting
'Hi, Pythonista!'
>>> del greeting
>>> greeting
Traceback (most recent call last):
...
NameError: name 'greeting' is not defined
Once you’ve run the del statement, the variable greeting is no longer available. If you try to access greeting, then you get a NameError because that variable doesn’t exist anymore.
Removing reference holders like variables is the primary goal of del. This statement doesn’t remove objects. In the following section, you’ll dive deeper into the actual and immediate effects of running a del statement.
Understanding the Effects of del
As you’ve learned, Python’s del statement deletes references from namespaces or data containers. It doesn’t remove concrete objects. For example, you can’t remove literals of built-in types using del: