A Python list can contain single numbers, characters, strings, and other lists. Sometimes, we want to separate the items or elements of a list into new lists based on a given parameter.
In this Answer, we'll take one such list and create a two-dimensional one. A two-dimensional list contains two sublists instead of a linear set of simple items like integers, floats, characters, or strings. Instead of being a linear set of items, a multi-dimensional list contains multiple levels of list items.
Therefore, we will have Python return a new list containing two others. One of these lists will contain only the even numbers from the original list, while the other will contain only the odd numbers.
Let’s create a list containing both even and odd numbers and assign it to the variable even_odd_list
.
even_odd_list = [2, 5, 78, 93, 3, 15, 34, 7, 8, 5, 1, 9]
Then, we'll create two empty lists: one to hold the even numbers and the other for odd numbers.
# a list for even numberseven = []# a list for odd numbersodd = []
Now use a for-loop
command to go through or traverse the loop, testing whether each number is divisible by the number 2. If it is, the number is even and goes into the list named even
. Otherwise, it's an odd number, and we put it into the list named odd
.
We can do this using Python's modulo (or remainder) operator %
using the example below.
We've included a side-by-side commentary so it's clear what's happening at each step.
for num in even_odd_list: # for each number in the original list,if num % 2 == 0: # if dividing the number by 2 gives a remainder of 0,even.append(num) # add the number to the list named even using Python's list append() methodelse:odd.append(num) # otherwise, append it to the list named odd
Since we now have two distinct lists—one containing even numbers and one containing odd numbers. We can create another list called separated
to hold both lists. We will append the even
list and the odd
list to this list.
# This empty list will now contain the populated even and odd listsseparated = []
We can now append even
and odd
to list named separated
.
# append the list named even to separatedseparated.append(even)# append the list named odd to separatedseparated.append(odd)
To print separated
list, we'll use a print statement—print(separated)
.
Now, here's the entire code together in one place. This is the easy way to separate a list of numbers into two lists of even and odd numbers, respectively.
even_odd_list = [2, 5, 78, 93, 3, 15, 34, 7, 8, 5, 1, 9]even = [] # a list for even numbersodd = [] # a list for odd numbersfor num in even_odd_list: # for each number in the original list,if num % 2 == 0: # if dividing the number by 2 gives a remainder of 0,even.append(num) # add the number to the list named even using Python's list append() methodelse:odd.append(num) # otherwise, append it to the list named odd# This empty list will now contain the populated even and odd listsseparated = []separated.append(even) # append the list named even to separatedseparated.append(odd) # append the list named odd to separated# prints out the contents of both lists and not just the variable namesprint(separated)
Another method to seperate even numbers and odd numbers from a list in Python is by using list comprehension method.
The method takes a list as input and outputs a tuple with two lists one containing even numbers and the other containing odd numbers.
We take a list numer_lst
containing even and odd numbers and pass it to the even_odd
method.
def even_odd(lst):even = [num for num in lst if num % 2 == 0] ## Create a new list containing only the elements divisible by 2.odd = [num for num in lst if num % 2 != 0] ## Create a new list containing only the elements divisible by 2 and remainder not equal to zero.return even, odd# Example Usage:numbers_lst = [2, 5, 78, 93, 3, 15, 34, 7, 8, 5, 1, 9]even, odd = even_odd(numbers_lst)print("Even numbers:", even)print("Odd numbers:", odd)