Write a program to delete all the odd numbers and negative numbers in a numeric list. for Eg;
if List=[0, -12, 13, 54, -20, 22, 86, 100, -102, 90, -31] then after removing all the odd numbers
and negative numbers the list become:
List=[0, 54, 22, 86, 100, 90]
Answers
Answered by
7
Answer:
def remove_odd(l):
return [e for e in l if e % 2 == 0]
remove_odd([0, -12, 13, 54, -20, 22, 86, 100, -102, 90, -31])
[0, 54, 22, 86, 100, 90]
Explanation:
MARK ME AS BRAINLIEST
Answered by
1
Python program that removes all odd and negative numbers from a list:
list = [0, -12, 13, 54, -20, 22, 86, 100, -102, 90, -31]
list = [x for x in list if x >= 0 and x % 2 == 0]
print(list)
- list is the input list containing both negative and odd numbers.
- We use a list comprehension to create a new list containing only the even and non-negative numbers from the input list.
- The x >= 0 condition checks if the number is non-negative, and the x % 2 == 0 condition checks if the number is even.
- The resulting list is then printed using the print() function.
In summary, the program removes all negative and odd numbers from the input list using a list comprehension that filters out these numbers based on their value and parity. The resulting list only contains even and non-negative numbers.
To learn more about list from the given link.
https://brainly.in/question/16086784
#SPJ3
Similar questions
English,
1 month ago
Science,
1 month ago
English,
2 months ago
Math,
9 months ago
Social Sciences,
9 months ago