Python Program to Split Even and Odd Elements into Two Lists

This Python program splits a list of numbers into two separate lists: one containing even elements and the other containing odd elements.

Problem Statement:

You are given a list of integers. Your task is to divide the elements of the list into two separate lists: one containing even numbers and the other containing odd numbers.

Python Program to Split Even and Odd Elements into Two Lists

def split_even_odd(numbers):
    even_list = []
    odd_list = []
    
    for num in numbers:
        if num % 2 == 0:
            even_list.append(num)
        else:
            odd_list.append(num)
    
    return even_list, odd_list

# Input
input_numbers = [12, 7, 45, 21, 8, 36, 78, 43]

# Call the function
even_numbers, odd_numbers = split_even_odd(input_numbers)

# Output
print("Even numbers:", even_numbers)
print("Odd numbers:", odd_numbers)

How it Works:

The program defines a functionsplit_even_odd that takes a list of numbers as input. Inside the function, it initializes two empty lists: even_list and odd_list. It then iterates through the input list and checks whether each number is even or odd using the modulo operator %. Depending on the result, the number is appended to the appropriate list. Finally, the function returns the two lists of even and odd numbers.

In the main part of the program, an input list called input_numbers is provided. The split_even_odd function is called with this input list, and the resulting even and odd number lists are stored in even_numbers and odd_numbers respectively. These lists are then printed as output.

Input and Output

Input

input_numbers = [12, 7, 45, 21, 8, 36, 78, 43]

Output

Even numbers: [12, 8, 36, 78]
Odd numbers: [7, 45, 21, 43]

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this python tutorial, you will learn how to Display Prime Numbers Between Two Intervals using the if and else...
In this python tutorial, you will learn how to Calculate Standard Deviation with built in functions of the python programming...
In this Python program, we will convert temperature values from Celsius to Fahrenheit. The Celsius and Fahrenheit scales are two...