In: Computer Science
Python: How would I write a function using list comprehensions to list of integers into two halves with the evens first, followed by odds? And on top of that, the numbers should be listed in their original order.
Code:
def evenodd(numbers):
print("Original List:",numbers)#printing the
origianl list
even=[i for i in numbers if(i%2==0)]#separating
evens into thelist
[even.append(i) for i in numbers
if(i%2==1)]#appending odd to the even list
print("Modified List:",even)#printing the
modified list
numbers=[]
n=int(input("No of numbers you want to enter:"))#Reading the no of
elements
print("Enter the elements of the list:")
for i in range(n):
numbers.append(int(input()))#Reading the input
and appending it into the list
evenodd(numbers)#calling the even odd function
Output:
Indentation: