Divide a list in half using Python.

January 15, 2021#python

Here's how you can divide a list into two pieces.

You can divide a list in half 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].