count in 'for ... in ...'
jeff at ccvcorp.com
Sat Nov 10 01:17:59 CET 2001
DeVerter at robinsonmechanical.com wrote:
> Try this.
> for s in my_list:
> print my_list.index(s), ".", s
> This way you don't need a counter variable.
Only problem is, this fails if there are any duplicate entries in my_list. index()
will always only find the first occurrence.
>>> breakfast = ['spam', 'eggs', 'bacon', 'spam', 'beans']
>>> for food in breakfast:
... print breakfast.index(food), food
More information about the Python-list