Python | Program to remove first occurrence of a given element in the list

Here, we are going to learn how to remove first occurrence of a given element in the list in Python. To remove an element from the list, we use list.remove(element). By IncludeHelp Last updated : June 22, 2023

Python program to remove first occurrence of a given element in the list: Given a list of the elements and we have to remove first occurrence of a given element.

Remove first occurrence of a given element in a Python list

To remove the first occurrence of a given element in a Python list, you can simply use the list.remove() method by passing the specified element as an argument. The method is called with this list and removes the element's first occurrence.

Syntax

The following is the syntax to remove first occurrence of the element from a Python list:

 list.remove(element)

Here,

  • list is the name of the list, from where we have to remove the element.
  • element an element/object to be removed from the list.

Example

Consider the below example with sample input & output values:

Input :
list [10, 20, 30, 40, 30]

function call to remove 30 from the list:
list.remove(30)

Output:
List elements after removing 30
list [10, 20, 40, 30]

Program

# Declaring a list
list1 = [10, 20, 30, 40, 30]

# print original list
print("Original list:", list1)

# removing 30 from the list
list1.remove(30)

# print the updated list
print("List after removing 30:", list1)

# removing 10 from the list
list1.remove(10)

# print the updated list
print("List after removing 10:", list1)

Output

Original list: [10, 20, 30, 40, 30]
List after removing 30: [10, 20, 40, 30]
List after removing 10: [20, 40, 30]

Python List Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.