• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Python Destroy Object – How to Delete Objects with del Keyword

Python Destroy Object – How to Delete Objects with del Keyword

February 14, 2022 Leave a Comment

In Python, we can destroy an object with the del keyword. The del keyword deletes objects in your Python code.

string = "this is a string"

del string

print(string)

#Output:
Error: string is not defined

When working in Python, sometimes it makes sense to be able to destroy an object or delete a variable in your program.

The Python del keyword allows us to delete objects. We can use the del keyword to destroy variables, user-defined objects, lists, items within lists, and dictionaries.

For example, let’s say we have a simple string variable. We can destroy it with the Python del keyword as shown in the following code. When you try to access the string variable after deleting it, you receive an error.

string = "this is a string"

del string

print(string)

#Output:
Error: string is not defined

How to Delete a Variable in Python with del Keyword

In Python, we can delete any variable with del.

Below are a handful of examples of how you can use del to delete a variable, list or dictionary.

string = "this is a string"
dict = {"name":"Kelly", "height":55}
list = [0,1,2,3]

del string
del dict
del list

print(string)
print(dict)
print(list)

#Output:
Error: string is not defined
Error: dict is not defined
Error: list is not defined

Removing Elements from a List in Python Using del Keyword

Another use of the del keyword is destroy elements from a list.

For example, we can remove the first element of a list using the del keyword easily as shown below.

list = [1,2,9,0,1,3]

del list[0]

print(list)

#Output:
[2,9,0,1,3]

We can also delete slices from lists with the del keyword.

list = [1,2,9,0,1,3]

del list[1:3]

print(list)

#Output:
[1,0,1,3]

Removing Key Value Pairs from Dictionaries Using del Keyword in Python

We can also use the del keyword to remove key value pairs from dictionary variables.

Below is an example of how you can delete a key value pair from a dictionary variable in Python.

dict = {"name":"Kelly", "height":55}

del dict["height"]

print(dict)

#Output:
{'name': 'Kelly'}

Destroying a User-Defined Object in Python with del Keyword

One last example I’d like to go over in this post is using the Python del keyword to destroy a user-defined object.

We can easily destroy a user-defined object as shown in the following Python code.

class Bank:
    Assets = 10
    def formatAssets(self):
        return "$" + str(assets)

del Bank

print(Bank)

#Output:
Error: Bank is not defined

Hopefully this article has been useful for you to understand how you can destroy objects in Python with the del keyword.

Other Articles You'll Also Like:

  • 1.  Get All Substrings of a String in Python
  • 2.  Append Multiple Elements to List Using Python
  • 3.  Get First Key and Value in Dictionary with Python
  • 4.  Convert True to 1 in Python
  • 5.  pandas groupby size – Get Number of Elements after Grouping DataFrame
  • 6.  Remove Duplicates from Sorted Array in Python
  • 7.  Sign Function in Python – Get Sign of Number
  • 8.  Create Empty String Variable in Python
  • 9.  pandas Absolute Value – Get Absolute Values in a Series or DataFrame
  • 10.  Creating a Random Color Turtle in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy