This Python program splits a list of numbers into two separate lists: one containing even elements and the other containing odd elements.
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 function
split_even_odd that takes a list of numbers as input. Inside the function, it initializes two empty lists:
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
odd_numbers respectively. These lists are then printed as output.
Input and Output
input_numbers = [12, 7, 45, 21, 8, 36, 78, 43]
Even numbers: [12, 8, 36, 78]
Odd numbers: [7, 45, 21, 43]