list indexing

Jeff Kowalczyk jtk at yahoo.com
Fri Aug 1 13:13:57 EDT 2003


> It's just index:
>>>> list = ['this', 'is', 'a', 'list']
>>>> for item in list:
> 	print list.index(item), item
> 0 this
> 1 is
> 2 a
> 3 list

But this only works for single instances, since it finds the first:
>>> li = 'this is a list with a repeated word in this list'.split()
>>> for item in li: print li.index(item), item
...
0 this
1 is
2 a
3 list
4 with
2 a
6 repeated
7 word
8 in
0 this
3 list

I wondered at first if there was a way to do it directly, but I usually
ended up making a companion range of indexes to zip(), and now will use
enumerate(). Was there a direct method that I never found?






More information about the Python-list mailing list