object's list index
William Meyer
wmmeyer at gmail.com
Fri Mar 3 07:48:36 EST 2006
Kent Johnson <kent <at> kentsjohnson.com> writes:
> In either case enumerate() is your friend. To find an
> item by identity:
>
> def index_by_id(lst, o):
> for i, item in enumerate(lst):
> if item is o:
> return i
> raise ValueError, "%s not in list" % o
>
> If you just want the index available inside the loop, this replaces your
> original loop:
> for i, object in enumerate(lst):
> print i
>
> Kent
Thanks, both you and Fredrik Lundh suggested enumerate, which seems like the
best solution. I just need the index inside the loop, but thanks again for both
solutions.
More information about the Python-list
mailing list