Answers for "divide a list in half python"

0

split list in half python

print(a_list)
length = len(a_list)
middle_index = length//2
first_half = a_list[:middle_index]
second_half = a_list[middle_index:]
print(first_half)
print(second_half)

>> [1, 2, 3, 4]
>> [1, 2]
>> [3, 4]
Posted by: Guest on May-23-2021
-1

divide list into equal parts python

def chunks(lst, n):
    """Yield successive n-sized chunks from lst."""
    for i in range(0, len(lst), n):
        yield lst[i:i + n]
Posted by: Guest on May-12-2021

Code answers related to "divide a list in half python"

Python Answers by Framework

Browse Popular Code Answers by Language