How do you separate odd and even numbers in python?

How can I separate the odd and even elements of list using recursion?

For example:

oddevenseparator([1]) => [[1], []]
oddevenseparator([2]) => [[], [2]]
oddevenseparator([1,2,3,4,5,6])=> [[1,3,5],[2,4,6]]

Separate odd and even number in a list to different two list

Separate odd and even numbers in a list into two different lists:

In this tutorial, we will discuss about separating odd and even numbers in a list into two different list.

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,…..

Example of odd number 1,3,5,7,…..

Here, we will use a modular operator to display odd or even numbersFirst.

if n%2==0,  n is a even number

if n%2==1,  n is a odd number

How do you separate odd and even numbers in python?
Separate odd and even number in a list

Program 1

Approach

  • To begin with, the user can allocate a length of the list of numbers using input() function.
  • Then, it is initialized by a list called “NumberList=[] ” which doen’t have any numbers.
  • Next, “for loop” is used to insert the numbers into the list.
  • The”For loop” is then used to append each number to the list NumberList=[].
  • Next, create two empty list: evenList=[] and oddList=[] for storage of even and odd numbers following separating from the initial mixed list
  • then, another “for loop” checks whether the number is even or odd in the list through a modular operator.
  • finally, odd and even numbers  are displayed into the different list

if n%2==0,  n is an even number, it is saved into an evenList=[]

if n%2==1,  n is an odd number, it is  saved into an oddList=[]

Program

numbers=[]  #Create a empty list for store number from user input
num=int(input("Enter number of elements: "))
for i in range(1,num+1):
    listElements=int(input("Enter telement %d: "%i))
    numbers.append(listElements)

evenList=[]  #list for store even number
oddList=[] #list for store odd number

for j in numbers:
    if j%2==0:
        evenList.append(j)
    else:
        oddList.append(j)

print("Even number list: ",evenList)#display separated even number list
print("Odd number list: ",oddList) #display separated odd number list

When the above code is executed, it produces the following results

Enter number of elements: 7
Enter telement 1: 25
Enter telement 2: 46
Enter telement 3: 67
Enter telement 4: 78
Enter telement 5: 89
Enter telement 6: 90
Enter telement 7: 87
Even number list: [46, 78, 90]
Odd number list:  [25, 67, 89, 87]

Suggested for you

for loop in Python language

if statements in Python language

Similar post

Python program check whether a number is odd or even

Python program to check a number is even or odd using the function

Python program to display even and odd numbers without if

Python program to display even and odd number in the given range

Python code to display all even and odd numbers from 1 to n

This is a Python Program to put the even and odd elements in a list into two different lists.

Problem Description

The program takes a list and puts the even and odd elements in it into two separate lists.

Problem Solution

1. Take in the number of elements and store it in a variable.
2. Take in the elements of the list one by one.
3. 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.
4. If the element is even, append it to a separate list and if it is odd, append it to a different one.
5. Display the elements in both the lists.
6. Exit.

Program/Source Code

Here is source code of the Python Program to put the even and odd elements in a list into two different lists. The program output is also shown below.

a=[]
n=int(input("Enter number of elements:"))
for i in range(1,n+1):
    b=int(input("Enter element:"))
    a.append(b)
even=[]
odd=[]
for j in a:
    if(j%2==0):
        even.append(j)
    else:
        odd.append(j)
print("The even list",even)
print("The odd list",odd)

Program Explanation

1. User must enter the number of elements and store it in a variable.
2. User must then enter the elements of the list one by one using a for loop and store it in a list.
3. Another for loop is used to traverse through the elements of the list.
4. The if statement checks if the element is even or odd and appends them to separate lists.
5. Both the lists are printed.

Runtime Test Cases

 
Case 1:
Enter number of elements:5
Enter element:67
Enter element:43
Enter element:44
Enter element:22
Enter element:455
The even list [44, 22]
The odd list [67, 43, 455]
 
Case 2:
Enter number of elements:3
Enter element:23
Enter element:44
Enter element:99
The even list [44]
The odd list [23, 99]

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Next Steps:

  • Get Free Certificate of Merit in Python Programming
  • Participate in Python Programming Certification Contest
  • Become a Top Ranker in Python Programming
  • Take Python Programming Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

How do you separate odd and even numbers in python?

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.

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.

How do you separate odd and even numbers in Python while loop?

2: Python Program to Split Even and Odd Numbers in Separate List using While loop. First of all, declare a number list, even number list and odd number list in python. Take the Input limit of the list from the user. Iterate for loop with input() function to take a single input number from the user.

How do you separate an even and odd array?

separate even and odd numbers in an array in C++.
Take input in the form of an integer or float array(Arr)..
Take evncnt=0 and oddcnt=0..
start for loop i=0 up to i<= length of array..
if (Arr[i]%2==0) then..
even[evncnt++]=Arr[i].
else odd[oddcnt++]=Arr[i].
end if..
end for..

How do you extract an odd number from a list in Python?

Given a list of numbers, write a Python program to print all odd numbers in given list. Using for loop : Iterate each element in the list using for loop and check if num % 2 != 0. If the condition satisfies, then only print the number.