Python del

BrandonDusch's avatar
Published Jul 11, 2022
Contribute to Docs

The del keyword is used to remove an object from the namespace of a Python shell or environment.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

del object

Depending on the object, the del keyword will do one of the following:

  • Plain deletion where the name of the object variable is removed from the local/global namespace.
  • Recursive deletion where any inner elements or attributes of the object (e.g., lists and classes) are deleted as well.
    • These inner elements can include list items and class attributes.
  • Slicing of specific inner elements in a sequence.

Codebyte Example

The following example showcases some uses of the del keyword:

Code
Output

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours