python exercises separating lists and removing the nums

Change your function to:

def filtra_ordinati(l1):
    l2 = []
    for y in range(1,len(l1)):
        if l1[y-1] < l1[y]:
            print(f'I will add {l1[y-1]} because it smaller then {l1[y]}')
            l2.append(l1[y-1])
    return l2

L1 = [1, 2, 5, 4, 7, 8, 3, 9]
print(f'list is {L1}')
print(filtra_ordinati(L1)) 

And you will see what you adding inside list and why.

Output will be:

list is [1, 2, 5, 4, 7, 8, 3, 9]
I will add 1 because it smaller then 2
I will add 2 because it smaller then 5
I will add 4 because it smaller then 7
I will add 7 because it smaller then 8
I will add 3 because it smaller then 9
[1, 2, 4, 7, 3]

The correct function is:

def f(l):
  l2 = l[:1]
  for el in l:
    if el > l2[-1]:
      l2.append(el)
  return l2

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top