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)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay