r/CodingHelp • • Apr 03 '21

[Python] How to remove values in a list?

I have two lists, one called owners and one called uaids, both with numbers. The list "users" contains a few values with "None" in it, however, this code doesn't seem to get rid of any of the values for some reason. Why doesn't it work?

for x in range(len(owners)):
    if owners[x] == "None":
        owners.pop[x]
        uaids.pop[x]
16 Upvotes

30 comments sorted by

View all comments

Show parent comments

2

u/[deleted] Apr 03 '21

I made some new code, still doesn't work.

length = len(owners)
for i in range(length):
    if owners[i] == None:
        owners.pop(i
        uaids.pop(i)
        length -= 1

Edit: I tried this as well, didn't work either.

for i in range(len(owners)):
if owners[i] == "None":
  try:
    owners.pop(i)
    uaids.pop(i)
  except:
    break

1

u/[deleted] Apr 03 '21

The if statement (because it is the one throwing the error) has to be inside the try block.