Define a function
split_list
that takes in a list of numbers and a numbera
and splits it into two sublists:list1
that contains numbers smaller than or equal toa
, and another list,list2
containing numbers greater than a.list1
andlist2
must be returned as elements of a tuple.
My code:
def split_list(lst, a):
list1 = []
list2 = []
for i in lst:
if i <= a:
list1.append(i)
lst.remove(i)
elif i > a:
list2.append(i)
lst.remove(i)
return (list1, list2)
Test code:
split_list([1, 10, 4, 9, 7, 2, 5, 8, 3, 4, 9, 6, 2], 5)
should give: ([1, 4, 2, 5, 3, 4, 2], [10, 9, 7, 8, 9, 6])
But I got ([1, 4, 5, 3, 2], [7, 9])
instead. Whats wrong with my code?