Removing List Items in Python: Deleting Elements from Lists

thumb_up 1  ·  sell Python list item removal, Deleting elements in Python lists, Removing items from a Python list

The list class methods remove() and pop() both can remove an item from a list. The difference between them is that remove() removes the object given as argument, while pop() removes an item at the given index.

Using the remove() Method

The following example shows how you can use the remove() method to remove list items −

 
list1 = ["Rohan", "Physics", 21, 69.75] print ("Original list: ", list1) list1.remove("Physics") print ("List after removing: ", list1)

It will produce the following output −

Original list: ['Rohan', 'Physics', 21, 69.75]
List after removing: ['Rohan', 21, 69.75]

Using the pop() Method

The following example shows how you can use the pop() method to remove list items −

 
list2 = [25.50, True, -55, 1+2j] print ("Original list: ", list2) list2.pop(2) print ("List after popping: ", list2)

It will produce the following output −

Original list: [25.5, True, -55, (1+2j)]
List after popping: [25.5, True, (1+2j)]

Using the "del" Keyword

Python has the "del" keyword that deletes any Python object from the memory.

Example

We can use "del" to delete an item from a list. Take a look at the following example −

 
list1 = ["a", "b", "c", "d"] print ("Original list: ", list1) del list1[2] print ("List after deleting: ", list1)

It will produce the following output −

Original list: ['a', 'b', 'c', 'd']
List after deleting: ['a', 'b', 'd']

Example

You can delete a series of consecutive items from a list with the slicing operator. Take a look at the following example −

 
list2 = [25.50, True, -55, 1+2j] print ("List before deleting: ", list2) del list2[0:2] print ("List after deleting: ", list2)

It will produce the following output −

List before deleting: [25.5, True, -55, (1+2j)]
List after deleting: [-55, (1+2j)]



The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back