Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Split List by book1978
from typing import Any, Iterable
def split_list(items: list[Any]) -> Iterable[list[Any]]:
return [items[:int(len(items) / 2 + 0.5)], items[int(len(items) / 2 + 0.5):]]
print("Example:")
print(list(split_list([1, 2, 3, 4, 5, 6])))
assert list(split_list([1, 2, 3, 4, 5, 6])) == [[1, 2, 3], [4, 5, 6]]
assert list(split_list([1, 2, 3])) == [[1, 2], [3]]
assert list(split_list(["banana", "apple", "orange", "cherry", "watermelon"])) == [
["banana", "apple", "orange"],
["cherry", "watermelon"],
]
assert list(split_list([1])) == [[1], []]
assert list(split_list([])) == [[], []]
print("The mission is done! Click 'Check Solution' to earn rewards!")
Oct. 24, 2022
Comments: