Python Forum
python if command for a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python if command for a list
#1
Hi,

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
Reply
#2
Removing elements from a list while iterating over that list isn't allowed. It confuses the iterator.

You could instead create a new list and then copy it back when done (or use itertools filterfalse()).
But in your case since you've already sorted and inserted, you could just use a slice to pull the elements you want.

lst = lst[:lst.index(b)+1]
Reply
#3
the problem with your code, as @bowlofred explained is that you should not remove elements from list while iterating over it.
Their suggested code however will work if b is not duplicate in the list. lst.index(number) will return the index of first occurrence of b in the list.
>>> number = 12500
>>> lst = [0, 12500, 50000, 150000]
>>> lst.append(number)
>>> lst.sort()
>>> lst = [num for num in lst if num <= number]
>>> lst
[0, 12500, 12500]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Thank you very much for your help guys.

The code works now.

@bowlofred @buran

Fid
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Passing List of Objects in Command Line Python usman 7 3,163 Sep-27-2020, 03:45 PM
Last Post: ndc85430
  Select correct item from list for subprocess command pythonnewbie138 6 3,291 Jul-24-2020, 09:09 PM
Last Post: pythonnewbie138
  Add list item into executable command zhome888 2 2,126 Dec-19-2019, 12:23 AM
Last Post: zhome888
  How to use list (structure) in the SQL Select command? pyuser1 2 2,913 Apr-27-2018, 02:33 PM
Last Post: pyuser1
  Saving shell command into a List steackfrite 4 3,924 Aug-31-2017, 11:28 AM
Last Post: steackfrite
  ssh remote command with list pythonnew 2 4,595 Jun-17-2017, 12:46 AM
Last Post: pythonnew

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020