Hi,
Please check the following code.
Appending and sorting are fine, but, the list output is incorrect.
I have included some example outputs with their inputs below. The values that shouldn't be in the list are in bold (any number greater than "number").
number: 5000
original list: [0, 12500, 50000, 150000]
append & sorted: [0, 5000, 12500, 50000, 150000]
final list: [0, 5000, 50000]
number: 16000
original list: [0, 12500, 50000, 150000]
append & sorted: [0, 12500, 16000, 50000, 150000]
final list: [0, 12500, 16000, 150000]
number: 45000
original list: [0, 12500, 50000, 150000]
append & sorted: [0, 12500, 45000, 50000, 150000]
final list: [0, 12500, 45000, 150000]
Any numbers above the last 2 numbers in the list are fine.
FYI, I also tried removing by using the pop() function. That didn't work either.
thanks in advance
Fid
Please check the following code.
number = 20000 lst = [0,12500,50000,150000] lst.append(number) lst.sort() for b in lst: if lst.index(b) > lst.index(number): lst.remove(b) print("final list:",lst)I am trying to append "number" to the list, then sort it in ascending order, then remove all of the numbers that are greater than "number".
Appending and sorting are fine, but, the list output is incorrect.
I have included some example outputs with their inputs below. The values that shouldn't be in the list are in bold (any number greater than "number").
number: 5000
original list: [0, 12500, 50000, 150000]
append & sorted: [0, 5000, 12500, 50000, 150000]
final list: [0, 5000, 50000]
number: 16000
original list: [0, 12500, 50000, 150000]
append & sorted: [0, 12500, 16000, 50000, 150000]
final list: [0, 12500, 16000, 150000]
number: 45000
original list: [0, 12500, 50000, 150000]
append & sorted: [0, 12500, 45000, 50000, 150000]
final list: [0, 12500, 45000, 150000]
Any numbers above the last 2 numbers in the list are fine.
FYI, I also tried removing by using the pop() function. That didn't work either.
thanks in advance
Fid