Divide a list in half using Python.

January 15, 2021

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].

Written by Murtaja Ziad, a full-stack web developer.
My website - Follow me on Twitter