Search code examples
pythonurllib2

python for each in list: item is not defined


I have list that is iterated over in for loop:

list = [a, b, c]

for each in list:
     print each

I receive the error:

name 'a' is not defined

Solution

  • Error you are getting is correct. You need to initialize your list like below.

    Change list = [a, b, c] to list = ['a', 'b', 'c']

    You need to have list of values instead of variables. Compiler is looking for variable "a" which is not defined hence throwing that error.