# Solution Exercise 31: Extract the sub-list of even numbers and that of odd numbers with python

Mar 30, 2023

#### Exercise 31

Write a Python program that retrieves the list of even integers and the list of odd integers from a list of numbers.

#### Solution

```def even_odd_lists(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

# Here's an example usage of the function:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
even_nums, odd_nums = even_odd_lists(numbers)
print("Even numbers:", even_nums)
print("Odd numbers:", odd_nums)
"""
Output:

Even numbers: [2, 4, 6, 8]
Odd numbers: [1, 3, 5, 7, 9]
"""```

Explanation:

1. This function takes a list 'numbers' as input: and returns two separate lists - one containing even integers, and the other containing odd integers.
2. We initialize the two lists: even_list = [] and odd_list = []
3. We then iterate over all elements of the list numbers: and we check if the element is even, we then add it to the eventilist, else we add it to the odd_list!
4. In this example: the input list contains integers from 1 to 9, and the output shows the even numbers in one list and the odd numbers in another list.

Younes Derfoufi
my-courses.net