Remove multiple elements from a list with Python

Configurare noua (How To)

Situatie

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

Backup

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]

Solutie

# 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]

Tip solutie

Permanent

Voteaza

(2 din 10 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?