0
A list contains so many odd and even numbers. How can I separate even numbers from list ant it stored in a new fresh list.
Which list is used for further operations.
2 Respuestas
+ 3
Hambire Ranjeet Sudarshan
Slick has given you a very concise answer, perfectly suitable.
I thought I might give an example using simpler syntax, but still employing Slick's concept
lst = [i for i in range(20)]
evens = []
odds = []
for i in lst:
if i%2==0:
evens.append(i)
else:
odds.append(i)
print(evens)
print(odds)
+ 3
in python:
say you have a list named, num_list
odds = [n in num_list if n % 2 == 1]
evens = [n in num_list if n % 2 == 0]