Divide a list in half using Python.

January 15, 2021#python

Divide a list in half using Python.

You can divide a list into two pieces using the following code..

def split_list(a_list):
    half = len(a_list) // 2
    return a_list[:half], a_list[half:]

first_piece, second_piece = split_list([1,2,3,4])
print(first_piece, second_piece)

As you see, this function will split a list into two lists the first one in the example will be [1, 2] and the second one will be [3, 4].


Share the post: