Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
One liner - math.ceil solution in Clear category for Split List by Pelmen323
import math
def split_list(items: list) -> list:
return [[i for i in items[:math.ceil(len(items)/2)]], [i for i in items[math.ceil(len(items)/2):]]]
#Returns 2 lists using list comprehension:
# 1st list starts from the beginning and ends at the middle of the list
# 2nd starts where the 1st one ends
# len(items/2)used to find the middle of the list
# math.ceil is used to round the number to a higher border if the len of list is not even
if __name__ == '__main__':
print("Example:")
print(split_list([1, 2, 3, 4, 5, 6]))
# These "asserts" are used for self-checking and not for an auto-testing
assert split_list([1, 2, 3, 4, 5, 6]) == [[1, 2, 3], [4, 5, 6]]
assert split_list([1, 2, 3]) == [[1, 2], [3]]
assert split_list([1, 2, 3, 4, 5]) == [[1, 2, 3], [4, 5]]
assert split_list([1]) == [[1], []]
assert split_list([]) == [[], []]
print("Coding complete? Click 'Check' to earn cool rewards!")
Aug. 22, 2021