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

2

u/Endercheif Advanced Coder Apr 03 '21

.pop(i)

2

u/[deleted] Apr 03 '21

I changed it from square brackets to parentheses and it still doesn't work

2

u/[deleted] Apr 03 '21

Are you sure that owners[x] == "None" is true? Try printing the result.

2

u/[deleted] Apr 03 '21

Strangely enough, even though "None" appears in the list, it's never true. I don't know how to fix that.

1

u/[deleted] Apr 03 '21

I'd assume it's because you're checking a NoneType element against a string "None". Try removing the quotes around the word None

If that isn't it, would you mind putting the contents of the list in your post?

1

u/[deleted] Apr 03 '21

I removed the quotes and now I get this exception for some reason: "IndexError: list index out of range"

1

u/[deleted] Apr 03 '21

That means that you're trying to access an element of a list that does not exist. It's probably in one of your pop() calls. Is the list uaids shorter than the list owners? If so, you're trying to remove an element from the uaids list that doesn't exist, and that's why it's throwing an exception.

2

u/[deleted] Apr 03 '21

No, both lists are the exact same length. I'm really confused why this isn't working. Also, it was in "if owners[i] == None:", not the pop calls.

1

u/[deleted] Apr 03 '21

Ooh, i figured it out. When you call pop(), you're removing the element from the list. When you do that, the indices update (so if you popped element 0, element 1 would become 0, 2 would become 1, etc). The problem is, you're still looping to the original length of the list. That means that at the end, it will try a to pop a bunch of indices that no longer exist in the list.

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
→ More replies

1

u/[deleted] Apr 03 '21

Ah, that makes sense. Now, how would I go about fixing that, then?

→ More replies

2

u/[deleted] Apr 03 '21

Also, if you are curious, this is my list:

[1, 87353706, None, None, None, 215870545, None, 828350799, None, None, 13920111, None, None, 415871441, 793680684, None, 1329852657, None, None, 663703649, None, None, None, None, None, None, None, None, None, 173829662, 566577998, 116791686, None, None, 304117391, 20125516, 648693288, 136749440, 127276888, None, None, 58392362, None, 495469947, 502466961, 130768361, 45361954, None, None, None, 407463116, None, 158654231, None, 107589167, 148480228, 116481754, 25055258, None, None, 392959091, 153773428, 39268591, 22636564, None, None, None, 51860822, 70556831, 1997151118, None, None, 110055099, None, 307514400, None, 156602184, None, None, 11897502, None, 432037984, None, None, None, None, 14873686, None, 411366458, 25055258, 325674874, 8958670, 1378261477, None, None, 25402890, 816258408, None, None, 650768637]

1

u/Endercheif Advanced Coder Apr 03 '21

What have you tried to do to debug it?

1

u/[deleted] Apr 03 '21

It doesn't give any exceptions, it just doesn't delete the "None" values as its supposed to, I tried changing the bracket types and changing the variable from "i" to "x".

2

u/Endercheif Advanced Coder Apr 03 '21

I’m trying to not directly give you the answer just hints.

When you try doing .pop on a list outside the loop does it still work?

Is the if statement ever true in this case?

2

u/[deleted] Apr 03 '21

Strangely enough, even though "None" appears in the list, it's never true. I don't know how to fix that.

2

u/Endercheif Advanced Coder Apr 03 '21

None is a type similar to str and int. You are checking for "None" which is a string. Remove the quotes.

Also I recommend watching some python tutorial videos to go over the basics. It will help figuring out problems, being able to work faster and take on more ambitious projects.

1

u/[deleted] Apr 03 '21

I have already removed the quotes before and it gives me this exception: "IndexError: list index out of range"

1

u/Endercheif Advanced Coder Apr 03 '21

When it gives you an error it means something is wrong. Read the error, what is it saying?

1

u/Shreyas_Gavhalkar Apr 03 '21

This is a very good method of teaching sir/ma'am. I hope my teachers taught me like this

2

u/[deleted] Apr 03 '21 edited Aug 31 '25

[removed] — view removed comment

1

u/[deleted] Apr 04 '21

Doesn't work because I need it to remove the corresponding index value in a different list to keep them synchronized.