Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python - Concatenate two lists element-wise
Next article icon

In this article, we will explore various methods to remove multiple elements from a list in Python. The simplest way to do this is by using aloop. A simplefor loop can also be used to remove multiple elements from a list.

Python
a=[10,20,30,40,50,60,70]# Elements to removeremove=[20,40,60]# Remove elements using a simple for loopres=[]forvalina:ifvalnotinremove:res.append(val)print(res)

Output
[10, 30, 50, 70]

Explanation

  • Iterate through each element in the original listaand adding it to result if it's not inremovelist.

Let's explore other different ways to remove multiple elements from list:

Using List Comprehension

List comprehensionis one of the most concise and efficient ways to remove multiple elements from a list.

Python
a=[10,20,30,40,50,60,70]# Elements to removeremove=[20,40,60]# Remove elements using list comprehensiona=[xforxinaifxnotinremove]print(a)

Output
[10, 30, 50, 70]

Explanation:

  • Iterate over each element inaand checking if it is not inremove list.
  • Elements not inremove list are included in the new list.

Using remove() in a Loop

remove() method removes the first occurrence of a specified element from the list. To remove multiple elements, we can use a loop to repeatedly callremove().

Python
a=[10,20,30,40,50,60,70]# Elements to removeremove=[20,40,60]# Remove elements using remove() in a loopforvalinremove:whilevalina:a.remove(val)print(a)

Output
[10, 30, 50, 70]

Explanation:

  • We iterate over each element inremove list.
  • The while loop make sure that all occurrences of each element are removed from the list.

Using filter() Function

filter()function can be used to remove elements from a list by providing a filtering condition and typically through alambda function.

Python
a=[10,20,30,40,50,60,70]# Elements to removeremove={20,40,60}# Remove elements using filtera=list(filter(lambdax:xnotinremove,a))print(a)

Output
[10, 30, 50, 70]

Explanation:

  • filter() function iterates over the listaand applying the lambda function.
  • If an element is not inremove list then include it in the filtered list.

Python program to Remove multiple elements from a List
Improve

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp