I am iterating over a list and I want to print out the index of the item if it meets a certain condition. How would I do this?
Example:
testlist = [1,2,3,5,3,1,2,1,6] for item in testlist: if item == 1: print position
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Hmmm. There was an answer with a list comprehension here, but it’s disappeared.
Here:
Example:
Update:
Okay, you want a generator expression, we’ll have a generator expression. Here’s the list comprehension again, in a for loop:
Now we’ll construct a generator…
and niftily enough, we can assign that to a variable, and use it from there…
And to think I used to write FORTRAN.