Home » Python programs

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).
Submitted by IncludeHelp, on July 21, 2018

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.

list.remove()

This method removes first occurrence given element from the list.

Syntax:

 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:

    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
list = [10, 20, 30, 40, 30]

# print list 
print "List element:"
for l in range(len(list)):
	print (list[l])

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

# print list after removing 30
print "List element after removing 30:"
for l in range(len(list)):
	print (list[l])

Output

    List element:
    10
    20
    30
    40
    30
    List element after removing 30:
    10
    20
    40
    30





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL








Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.