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. Show ExampleInput: [1, 2, 3, 4, 5, 9, 8, 6] Output Even lists: [2, 4, 8, 6] Odd lists: [1, 3, 5, 9] AlgorithmStep 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) OutputEnter 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]
Updated on 30-Jul-2019 22:30:23
I was asked this question in a tcs codevita interview. Given an array
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
and other list
I was not able to write this code in one line. Can anyone tell me how to solve this in one line?
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:
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
answered Aug 25, 2018 at 6:18
k.wahomek.wahome 8624 silver badges14 bronze badges 5 You can slice the list with a step of 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
answered Jul 4, 2019 at 15:33
Smart ManojSmart Manoj 4,5624 gold badges28 silver badges52 bronze badges 1
answered Jul 13, 2020 at 13:58
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,…..
|