Which of the following can delete an element from a list without index of the element?

Why use Lists?

Sometimes, there may be situations where you need to handle different types of data at the same time. For example, let’s say, you need to have a string type element, an integer and a floating-point number in the same collection. Now, this is fairly impossible with the default data types that are available in other programming languages like C & C++. In simple words, if you define an array of type integer, you can only store integers in it. This is where Python has an advantage. With its list collection data type, we can store elements of different data types as a single ordered collection!

Now that you know how important lists are, let’s move on to see what exactly are Lists and how to remove elements from list!

Delete Element From List in Python

Python Python List

Created: October-12, 2021 | Updated: December-02, 2021

If you are working with the list, and you want to remove an element from the list without making lots of effort and want to save your time, you can use the Python built-in functions to remove that particular element from the list.

In this article, we will learn how to delete an element in a list by value in a very simple way. When we work with list operations, there are times when we need to delete certain elements from the list permanently. Fortunately, Python has many different yet simple and convenient ways to remove those particular elements from the list.

There are various ways to delete or remove the element from the list, for example, remove[], del[], and pop[] methods etc.

How to remove an element from a list in Python

Python provides the following methods to remove one or multiple elements. We can delete the elements using the del keyword by specifying the index position. Let's understand the following methods.

  • remove[]
  • pop[]
  • clear[]
  • del
  • List Comprehension - If specified condition is matched.

The remove[] method

The remove[] method is used to remove the specified value from the list. It accepts the item value as an argument. Let's understand the following example.

Example -

Output:

The list is: ['Joseph', 'Bob', 'Charlie', 'Bob', 'Dave'] After removing element: ['Bob', 'Charlie', 'Bob', 'Dave']

If the list contains more than one item of the same name, it removes that item's first occurrence.

Example -

Output:

The list is: ['Joseph', 'Bob', 'Charlie', 'Bob', 'Dave'] After removing element: ['Joseph', 'Charlie', 'Bob', 'Dave']

The pop[] method

The pop[] method removes the item at the specified index position. If we have not specified the index position, then it removes the last item from the list. Let's understand the following example.

Example -

Output:

The list is: ['Joseph', 'Bob', 'Charlie', 'Bob', 'Dave'] After removing element: ['Joseph', 'Bob', 'Charlie', 'Dave'] After removing element: ['Joseph', 'Bob', 'Charlie']

We can also specify the negative index position. The index -1 represents the last item of the list.

Example -

Output:

The list is: ['Joseph', 'Bob', 'Charlie', 'Bob', 'Dave'] After removing element: ['Joseph', 'Bob', 'Charlie', 'Dave']

The clear[] method

The clear[] method removes all items from the list. It returns the empty list. Let's understand the following example.

Example -

Output:

[10, 20, 30, 40, 50, 60] []

The del statement

We can remove the list item using the del keyword. It deletes the specified index item. Let's understand the following example.

Example -

Output:

[10, 20, 30, 40, 50, 60] [10, 20, 30, 40, 50] [10, 20, 30, 40]

It can delete the entire list.

Output:

Traceback [most recent call last]: File "C:/Users/DEVANSH SHARMA/PycharmProjects/Practice Python/first.py", line 14, in print[list1] NameError: name 'list1' is not defined

We can also delete the multiple items from the list using the del with the slice operator. Let's understand the following example.

Example -

Output:

[10, 20, 30, 40, 50, 60] [10, 40, 50, 60] [60] []

Using List Comprehension

The list comprehension is slightly different way to remove the item from the list. It removes those items which satisfy the given condition. For example - To remove the even number from the given list, we define the condition as i % 2 which will give the reminder 2 and it will remove those items that's reminder is 2.

Let's understand the following example.

Example -

Output:

[20, 34, 40, 60] [11, 45]
Next TopicHow to Round number in Python


← prev next →


Video liên quan

Bài Viết Liên Quan

Bài mới nhất

Chủ Đề