Welcome, guest | Sign In | My Account | Store | Cart

my_list.index(element) returns the index of the element using common comparision (as in == or __eq__() or __cmp__()). If you need to find an element on the list using identity comparing (is) then this function can do it for you

Python, 2 lines
1
2
def index_id(a_list, elem):
    return (index for index, item in enumerate(a_list) if item is elem).next()

This approach iterates on the list until it finds the first element that matches and returns it without searching the rest of the list.

Created by nosklo on Sat, 16 Aug 2008 (MIT)
Python recipes (4591)
nosklo's recipes (5)

Required Modules

  • (none specified)

Other Information and Tasks