Extract Even and odd number from a given list in Python

Extracting EVEN and ODD numbers from the list: In this tutorial, we will learn how to extract/split EVEN and ODD numbers from a given list in Python programming language? By Bipin Kumar Last updated : June 26, 2023

The number that can be fully divided by 2 is known as an EVEN number and if the number is not divisible by 2 then it is known as an ODD number.

Here, we are given a Python list by the user which may be a mixture of EVEN and ODD numbers, and based on the concept of EVEN and ODD, we will split the list into two lists one will contain only EVEN numbers and another will contain only ODD numbers. Before going to do this task, we will learn how to check whether the given number is EVEN or ODD in Python.

Extract EVEN and ODD numbers from a list using for ... in loop

In this approach, to extract the EVEN and ODD numbers, we are using the for ... in loop and condition to check EVEN and ODD numbers. Consider the below implementation for this approach.

Algorithm

  1. Take the input in the form of a list.
  2. Create two empty lists to store the even and odd number which will be extracted from the given list.
  3. Check each element of the given list.
    1. If it is an EVEN number, then add this to one of the lists from the above-created list by using the append method.
    2. If it is an ODD number, then add this to another list from the above-created list by using the append method.
  4. Print both lists which will be our required list.

Example

# input the list
A = list(map(int, input("Enter elements of List: ").split()))

# create two empty lists to store EVEN and ODD elements
B = []
c = []

for j in A:
    if j % 2 == 0:
        B.append(j)
    else:
        c.append(j)

print("List of even number: ", B)
print("List of odd number: ", c)

Output

Enter elements of List: 6 4 7 45 7 6 7 9 2 1
List of even number: [6, 4, 6, 2]
List of odd number: [7, 45, 7, 7, 9, 1]

Here, we have used the list.append() method to add even numbers to list B and odd numbers to list C.

Extract EVEN and ODD numbers from a list using lambda expressions

With the help of Python lambda expression, filter(), and list() method - you can easily extract and create lists for EVEN and ODD numbers. Consider the below example for this implementation.

# list
numList = [10, 20, 30, 11, 22, 33, 43, 81, 80]

# Extracting EVEN and ODD numbers from the list
# using Python Lambda Expressions
oddList = list(filter(lambda x: (x % 2 != 0), numList))
evenList = list(filter(lambda x: (x % 2 == 0), numList))

print("Original list:", numList)
print("List with even numbers:", oddList)
print("List with odd numbers:", evenList)

Output

Original list: [10, 20, 30, 11, 22, 33, 43, 81, 80]
List with even numbers: [11, 33, 43, 81]
List with odd numbers: [10, 20, 30, 22, 80]

Extract EVEN and ODD numbers from a list using list comprehension

In this approach, we are using list compression for extracting even and odd numbers from the list and creating new lists with these extracted numbers.

# list
numList = [10, 20, 30, 11, 22, 33, 43, 81, 80]

# Extracting EVEN and ODD numbers from the list
# using List Comprehension
oddList = [x for x in numList if x % 2 == 1]
evenList = [x for x in numList if x % 2 == 0]

print("Original list:", numList)
print("List with even numbers:", oddList)
print("List with odd numbers:", evenList)

Output

Original list: [10, 20, 30, 11, 22, 33, 43, 81, 80]
List with even numbers: [11, 33, 43, 81]
List with odd numbers: [10, 20, 30, 22, 80]

Python List Programs »


Related Programs

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.