Write a program to remove the element 3 from the given list 1 2 3 4 5 6

Python: Remove all elements from a given list present in another list using lambda

Last update on November 27 2020 13:22:42 [UTC/GMT +8 hours]

Remove multiple elements from a list in Python

Given a list of numbers, write a Python program to remove multiple elements from a list based on the given condition.

Example:

Input: [12, 15, 3, 10] Output: Remove = [12, 3], New_List = [15, 10] Input: [11, 5, 17, 18, 23, 50] Output: Remove = [1:5], New_list = [11, 50]

Multiple elements can be deleted from a list in Python, based on the knowledge we have about the data. Like, we just know the values to be deleted or also know the indexes of those values. Let’s see different examples based on different scenario.

Example #1: Let’s say we want to delete each element in the list which is divisible by 2 or all the even numbers.

Python3




# Python program to remove multiple
# elements from a list
# creating a list
list1 = [11, 5, 17, 18, 23, 50]
# Iterate each element in list
# and add them in variable total
for ele in list1:
if ele % 2 == 0:
list1.remove[ele]
# printing modified list
print["New list after removing all even numbers: ", list1]

Output:



New list after removing all even numbers: [11, 5, 17, 23]

Example #2: Using list comprehension
Removing all even elements in a list is as good as only including all the elements which are not even[ i.e. odd elements].

Python3




# Python program to remove multiple
# elements from a list
# creating a list
list1 = [11, 5, 17, 18, 23, 50]
# will create a new list,
# excluding all even numbers
list1 = [ elem for elem in list1 if elem % 2 != 0]
print[*list1]

Output:

11 5 17 23

Example #3: Remove adjacent elements using list slicing
Below Python code remove values from index 1 to 4.

Python3




# Python program to remove multiple
# elements from a list
# creating a list
list1 = [11, 5, 17, 18, 23, 50]
# removes elements from index 1 to 4
# i.e. 5, 17, 18, 23 will be deleted
del list1[1:5]
print[*list1]

Output:

11 50

Example #4: Using list comprehension
Let’s say the elements to be deleted is known, instead of the indexes of those elements. In this case, we can directly eliminate those elements without caring about indexes which we will see in next example.

Python3




# Python program to remove multiple
# elements from a list
# creating a list
list1 = [11, 5, 17, 18, 23, 50]
# items to be removed
unwanted_num = {11, 5}
list1 = [ele for ele in list1 if ele not in unwanted_num]
# printing modified list
print["New list after removing unwanted numbers: ", list1]

Output:

New list after removing unwanted numbers: [17, 18, 23, 50]

Example #5: When index of elements is known.
Though indexes of elements in known, deleting the elements randomly will change the values of indexes. Hence, it is always recommended to delete the largest indices first. Using this strategy, index of smaller values will not be changed. We can sort the list in reverse order and delete the elements of list in descending order.

Python3




# Python program to remove multiple
# elements from a list
# creating a list
list1 = [11, 5, 17, 18, 23, 50]
# given index of elements
# removes 11, 18, 23
unwanted = [0, 3, 4]
for ele in sorted[unwanted, reverse = True]:
del list1[ele]
# printing modified list
print [*list1]

Output:

5 17 50

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




Article Tags :
Python
Python Programs
School Programming
Python list-programs
python-list
Practice Tags :
python-list
Read Full Article

Python | Remove element from given list containing specific digits

Given a list, the task is to remove all those elements from list which contains the specific digits.

Examples:

Input: lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16] no_delete = ['2', '3', '4', '0'] Output: [1, 5, 6, 7, 8, 9, 11, 15, 16] Explanation: Numbers 2, 3, 4, 10, 12, 13, 14 contains digits from no_delete, therefore remove them. Input: lst = [1, 2, 3, 4, 5, 6, 7, 8, 13, 15, 16] no_delete = {'6', '5', '4', '3'} Output: [1, 2, 7, 8, 9, 10, 11, 12] Explanation: Numbers 3, 4, 5, 6, 13, 14, 15, 16 contains digits from no_delete, therefore remove them.


Below are some methods to do the task.

Method #1: Using Iteration




# Python code to remove all those elements
# from list which contains certain digits
# Input List Initialisation
Input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16]
# Numbers to delete
no_delete = [1, 0]
# Output List Initialisation
Output = []
# Using iteration to remove all the elements
for elem in Input:
flag = 1
temp = elem
while elem > 0:
rem = elem % 10
elem = elem//10
if rem in no_delete:
flag = 0
if flag == 1:
Output.append[temp]
# Printing Output
print["Initial list is :", Input]
print["Delete list :", no_delete]
print["List after removing elements is :", Output]
Output:

Initial list is : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16] Delete list : [1, 0] List after removing elements is : [2, 3, 4, 5, 6, 7, 8, 9]


Method #2: Using List comprehension and any[] function




# Python code to remove all those elements from list
# which contains certain digits
# Input List Initialisation
Input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16]
# Numbers to delete
no_delete = ['2', '3', '4', '0']
# using list comprehension and any[]
Output = [a for a in Input if not
any[b in no_delete for b in str[a]]]
# Printing Output
print["Initial list is :", Input]
print["Delete list :", no_delete]
print["List after removing elements is :", Output]
Output: Initial list is : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16] Delete list : ['2', '3', '4', '0'] List after removing elements is : [1, 5, 6, 7, 8, 9, 11, 15, 16]


Method #3: Using List comprehension and set[]




# Python code to remove all those elements from list
# which contains certain digits
# Input List Initialisation
Input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16]
# Numbers to delete
no_delete = {'6', '5', '4', '3'}
# Using list comprehension and set
Output = [x for x in Input
if not no_delete & set[str[x]]]
# Printing Output
print["Initial list is :", Input]
print["Delete list :", no_delete]
print["List after removing elements is :", Output]
Output: Initial list is : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16] Delete list : {'3', '4', '6', '5'} List after removing elements is : [1, 2, 7, 8, 9, 10, 11, 12]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




Article Tags :
Python
Python Programs
Python list-programs
Read Full Article

Python remove[] method

Python removes [] method is a built-in method available with the list. It helps to remove the given very first element matching from the list.

Syntax:

list.remove[element]

The element that you want to remove from the list.

ReturnValue

There is no return value for this method.

Tips for using remove[] method:

Following are the important points to remember when using remove [] method:

  • When the list has duplicate elements, the very first element that matches the given element will be removed from the list.
  • If the given element is not present in the list, it will throw an error saying the element is not in the list.
  • The remove [] method does not return any value.
  • The remove [] takes the value as an argument, so the value has to pass with the correct datatype.

Example: Using remove[] method to remove an element from the list

Here is a sample list that i have

my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya']

The list has elements of date-types string and number. The list has duplicate elements like number 12 and string Riya.

my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya'] my_list.remove[12] # it will remove the element 12 at the start. print[my_list] my_list.remove['Riya'] # will remove the first Riya from the list print[my_list] my_list.remove[100] #will throw an error print[my_list]

Output:

['Siya', 'Tiya', 14, 'Riya', 12, 'Riya'] ['Siya', 'Tiya', 14, 12, 'Riya'] Traceback [most recent calllast]: File "display.py", line 9, in my_list.remove[100] ValueError: list.remove[x]: x not in the list

Python List

In this tutorial, we'll learn everything about Python lists: creating lists, changing list elements, removing elements, and other list operations with the help of examples.

Python Sets

In this tutorial, you'll learn everything about Python sets; how they are created, adding or removing elements from them, and all operations performed on sets in Python.

Video liên quan

Bài mới nhất

Chủ Đề