Test a list

Tim Chase python.list at tim.thechases.com
Wed Mar 20 19:36:01 CET 2013

On 2013-03-20 11:15, Ana Dionísio wrote:
> t= [3,5,6,7,10,14,17,21]
> Basically I want to print Test 1 when i is equal to an element of
> the list "t" and print Test 2 when i is not equal:
> while i<=25:
>     if i==t[]:
>        print "Test1"
>     else:
>        print "Test2"
> What is missing here for this script work?

Well, your code never increments "i", so it will loop forever; you
also don't subscript "t" with anything, so you have invalid syntax
there; you also don't have any values in the list that actually match
their 0-indexed offset, so even if your code was correct, it would
still (correctly) return Test2 for everything.

This sounds a bit like homework, but the Pythonic way would likely
iterate over the data and its enumeration:

  for index, value in enumerate(t):
    if index == value: # compare index & value accordingly

If you need to have the index start at 1 (or some other value)
instead of 0, and you're running Python2.6+, you can pass the initial
index to enumerate().  Otherwise, you have to do the math in your


More information about the Python-list mailing list