Print the even and odd integers of the integer in a separate line in python


In this program we create a user input list and the elements are mixture of odd and even elements. Our task is to split these list into two list. One contains odd number of element and another is even number of elements.

Example

Input: [1, 2, 3, 4, 5, 9, 8, 6]
Output
Even lists: [2, 4, 8, 6]
Odd lists: [1, 3, 5, 9]

Algorithm

Step 1 : create a user input list.
Step 2 : take two empty list one for odd and another for even.
Step 3 : then traverse each element in the main list.
Step 4 : every element is divided by 2, if remainder is 0 then it’s even number and add to the even list, otherwise its odd number and add to the odd list.

Example Code

# Python code to split into even and odd lists 
# Funtion to split 
def splitevenodd(A): 
   evenlist = [] 
   oddlist = [] 
   for i in A: 
      if (i % 2 == 0): 
         evenlist.append(i) 
      else: 
         oddlist.append(i) 
   print("Even lists:", evenlist) 
   print("Odd lists:", oddlist) 
  
# Driver Code 
A=list()
n=int(input("Enter the size of the First List ::"))
print("Enter the Element of First  List ::")
for i in range(int(n)):
   k=int(input(""))
   A.append(k)
splitevenodd(A) 

Output

Enter the size of the First List :: 8
Enter the Element of First  List ::
1
2
3
4
5
9
8
6
Even lists: [2, 4, 8, 6]
Odd lists: [1, 3, 5, 9]

Print the even and odd integers of the integer in a separate line in python

Updated on 30-Jul-2019 22:30:23

  • Related Questions & Answers
  • Java program to split the Even and Odd elements into two different lists
  • Python Program to Put Even and Odd elements in a List into Two Different Lists
  • C# program to split the Even and Odd integers into different arrays
  • Swap Even Index Elements And Odd Index Elements in Python
  • Sorting odd and even elements separately JavaScript
  • Adding two Python lists elements
  • C program to store even, odd and prime numbers into separate files
  • Python program to print all the common elements of two lists.
  • Program to find minimum difference between two elements from two lists in Python
  • Python program to Count Even and Odd numbers in a List
  • Program to interleave list elements from two linked lists in Python
  • Java Program to Split a list into Two Halves
  • Python Program to Merge Two Lists and Sort it
  • Program to split lists into strictly increasing sublists of size greater than k in Python
  • Missing even and odd elements from the given arrays in C++

I was asked this question in a tcs codevita interview. Given an array

a = [1,2,3,4,5,6,7,8,9,10]

you have to write a one line code in Python such that you get 2 different array/lists where one will contain odd numbers and the other will contain even numbers. i.e one list

odd = [1,3,5,7,9]

and other list

even =[2,4,6,8,10]

I was not able to write this code in one line. Can anyone tell me how to solve this in one line?

Print the even and odd integers of the integer in a separate line in python

tripleee

164k27 gold badges244 silver badges296 bronze badges

asked Aug 25, 2018 at 5:55

0

You can use two list comprehensions in one line:

odd, even = [el for el in a if el % 2==1], [el for el in a if el % 2==0]

print(odd, even)
#([1, 3, 5, 7, 9], [2, 4, 6, 8, 10])

answered Aug 25, 2018 at 6:06

abcabc

11k2 gold badges22 silver badges46 bronze badges

List comprehension holds the answer. But rather than comprehend on both even and odd list construction, pop one kind (even in this case) from you original list a and put in it's list and what you have left in a will be the other kind (odd):

>>> even, odd = [a.pop(index) for index, item in enumerate(a) if item % 2 == 0], a
>>> print(even,odd)
[2, 4, 6, 8, 10] [1, 3, 5, 7, 9]

answered Aug 25, 2018 at 6:18

Print the even and odd integers of the integer in a separate line in python

k.wahomek.wahome

8624 silver badges14 bronze badges

5

You can slice the list with a step of 2:

odd, even = a[::2], a[1::2]

answered Aug 25, 2018 at 6:05

blhsingblhsing

81.2k6 gold badges62 silver badges94 bronze badges

6

Using key,to get in a single list

print(sorted(j,key=lambda x:(x%2,-a.index(x))))

answered Jul 4, 2019 at 15:33

Smart ManojSmart Manoj

4,5624 gold badges28 silver badges52 bronze badges

1

Using list comprehension:    

def evenodd(myl): 
       evenlist = [num for num in myl if num % 2 == 0] 
       oddlist = [num for num in myl if num % 2 == 1]  
       print("Even numbers:", evenlist) 
       print("Odd numbers:", oddlist) 
      
    mylist=list()
    n=int(input("Enter the size of the  List:"))
    print("Enter the numbers:")
    for i in range(int(n)):
       k=int(input(""))
       mylist.append(k)
    evenodd(mylist) 

answered Jul 13, 2020 at 13:58

Print the even and odd integers of the integer in a separate line in python

1

How do you print even and odd numbers separately in Python?

Python Program to Print Even and Odd Numbers in a List.
num_list=[].
n=int(input("Enter the Starting of the range:")).
k=int(input("Enter the Ending of the range:")).
for i in range(n,k):.
num_list. append(i).
print("Original Number List:", num_list).
even_list=[].
odd_list=[].

How do you separate an even and odd list in Python?

Python Program to Split Even and Odd Elements into Two Lists.
Take in the number of elements and store it in a variable..
Take in the elements of the list one by one..
Use a for loop to traverse through the elements of the list and an if statement to check if the element is even or odd..

How do you print even numbers on one line in Python?

15 ways to print even numbers in Python.
With just one print. The simplest way is: print(0,2,4,6,8,10).
For loop. The first method that comes into my mind: for i in range(0,11,2): ... .
For and % for i in range(11): ... .
Generators and % print([i for i in range(11) if i%2 == 0]).
Generators and Binary. ... .
Bitwise AND..

How do you separate odd and even numbers?

When you divide a number by two and if the balance is zero, it is an even number. When a number is divided by two with a remaining balance of 1, then it's an odd number. Example of even number 2,4,6,8,…..