Given an integer list and a key, I am required to group the list by greater than or equal to the key, or less than the key.
For example, When
key = 6 and lst = [1, 1, 1, 0, 0, 6, 10, 5, 10]
Output should be:
[[1, 1, 1, 0, 0], [6, 10], [5], [10]]
A new sublist must be created if while looping a new condition is met. When the process of grouping is over, the sublists are placed into a list. If the list is empty, it returns an empty list.
def group_ints(lst, key):
greater_or_equal = []
lessthan = []
result = []
if lst == None:
return []
else:
for i in lst:
if i >= key:
greater_or_equal.append(i)
elif i < key:
lessthan.append(i)
if greater_or_equal:
result.append(greater_or_equal)
if lessthan:
result.append(lessthan)
return result
result.append(greater_or_equal)andresult.append(lessthan)are the only two places in the code that add something toresult. So yes, of courseresultis only going to have, at most, two things in it...