DEV Community

Murtaja Ziad
Murtaja Ziad

Posted on • Originally published at blog.murtajaziad.xyz on

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)
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)