In case you are using Python3 (you must be, otherwise the code works fine),
a is a string, not the variable itself.
You can verify it doing
list1 = [0,1,0,1,0,1]
list2 = [1,0,1,0,1,0]
a = input("Which list do you want to search for?")
print(type(a))
which gives you
<class 'str'>
So you can try this instead:
for item in range(len(vars()[a])):
print(item)
which produces
0
1
2
3
4
5
From the official docs:
vars([object])
Return the dict attribute for a module, class, instance, or any other object with a dict attribute.
Objects such as modules and instances have an updateable dict attribute; however, other objects may have write restrictions on their
dict attributes (for example, classes use a dictproxy to prevent direct dictionary updates).
Without an argument, vars() acts like locals(). Note, the locals dictionary is only useful for reads since updates to the locals
dictionary are ignored.
As an alternative, you can use eval
for item in range(len(eval(a))):
print(item)
but it's not recommended (see the official docs) since
This function can also be used to execute arbitrary code
Note: there are several ways to solve this problem. For example, as pointed out below, you could decide which list to use by checking the input with an if and hard-code the list in either branch taken. The problem with this option is that it does not fit a higher/variable/dynamic number of lists.
Another approach could be to store the lists in a dictionary and look them up using the input key, as suggested in another answer. This one and mine have the benefit of being able to handle any number of lists, not just a few and even lists created dynamically in the namespace.
ais a number or a string, not the name of your variableprint(a)