Home > Back-end >  Getting "unknown variable x" or "ValueError" when putting a variable with the &q
Getting "unknown variable x" or "ValueError" when putting a variable with the &q

Time:11-14

Why is the name(variable) being considered "unknown variable 'x' " by the interpreter and not be taken as the "value" it has.`

list_of_names = [1, 1, 1, 1, 1, 2, 2, 2, 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]
name = 0
for name in range(100):
    counter = list_of_names.count(name)
    while counter > 1:
            list_of_names.remove(name)

print(list_of_names)

` Output shown:

Traceback (most recent call last):
  File "x", line 6, in <module>
    list_of_names.remove(name)
ValueError: list.remove(x): x not in list

Process finished with exit code 1

CodePudding user response:

while counter > 1:
        list_of_names.remove(name)

That loop will (try) to run forever, because you're not changing counter inside the loop.

If it started at, say, 5, then it will be 5 forever because nothing in the loop changes it. Eventually you will remove the last occurrence of name from the list, and then the next loop iteration will crash.

CodePudding user response:

OP if you're trying to remove duplicates, use:

list(set(list_of_names))

set(iterable) ignores duplicates.


Here's the documentation for list.remove:

s.remove(x) remove the first item from s where s[i] is equal to x (3)

Note footnote (3)

  1. remove raises ValueError when x is not found in s.

Here's an example of what's happening:

s = [1, 2, 3]
s.remove(1)
print(s) # [2, 3]
s.remove(1) # ValueError because s does not have a 1

I'm not sure what you're trying to accomplish with your code, but this is why you're getting a ValueError.

CodePudding user response:

Because there is no name inside the list_of_names when the remove is being called. I don't understand what you are trying to achieve with that code. But you can avoid that exception by adding a little check.

list_of_names = [1, 1, 1, 1, 1, 2, 2, 2, 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]
name = 0
for name in range(100):
    counter = list_of_names.count(name)
    while counter > 1 and name in list_of_names: #Add a check here to avoid the exception
            list_of_names.remove(name)

print(list_of_names)

CodePudding user response:

When all the name is removed from the list, the while loop does not terminate and still tries to remove the name which is not present in the list.

You need to decrease the value of counter with in the while loop, so that it terminates when there is no name in the list.

list_of_names = [1, 1, 1, 1, 1, 2, 2, 2, 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]
name = 0
for name in range(100):
    counter = list_of_names.count(name)
    while counter > 1:
        counter -= 1
        list_of_names.remove(name)

print(list_of_names)
[1, 2, 3]

CodePudding user response:

I found some problems:

First if all, the main reason you are getting issues is because your counter statement doesn't actually decrease because it is outside of the while statement.

I got it to work with this code:

list_of_names = [1, 1, 1, 1, 1, 2, 2, 2, 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]
name = 0
for name in list_of_names:
  while True:
    counter = list_of_names.count(name)
    if counter < 1:
      break
    print(f"Counter: {counter}")
    print(name in list_of_names)
    print(name)
    print(list_of_names)
    list_of_names.remove(name)
            

print(list_of_names)
  • Related