For Counter Variable

Chris Angelico rosuav at gmail.com
Sun Sep 23 12:52:43 EDT 2012


On Mon, Sep 24, 2012 at 2:36 AM, jimbo1qaz <jimmyli1528 at gmail.com> wrote:
> Am I missing something obvious, or do I have to manually put in a counter in the for loops? That's a very basic request, but I couldn't find anything in the documentation.

You mean, if you want the indices as well as the values? Try the
enumerate() function:

my_list = ["foo", "bar", "quux"]
for idx,val in enumerate(my_list):
    print("Element "+str(idx)+" is: "+val)

ChrisA



More information about the Python-list mailing list