index of list of lists

Daniel Fetchinson fetchinson at googlemail.com
Wed Apr 16 22:25:51 EDT 2008


> yes, there's a thread with the same title, but I believe mine is more
> appropriate title.
> so, as much as I search on the web, read manuals, tutorials, mail-lists
> (including this one) I cannot figure it out how to search a string in a
> list of lists.
> like this one:
>
> someList = [['somestring', 1, 2], ['oneother', 2, 4]]
>
> I want to search "somestring" in someList which is in practice a list
> of aprox. 200 lists. (hey, I'm a newbie python programmer, don't judge
> me).
> is the list.index the wrong approach?
> should I use numpy, numarray, something else?
> can anyone, be kind and help me with this?

someList = [['somestring', 1, 2], ['oneother', 2, 4]]
for alist in someList:
    if alist[0] == 'somestring':
        print "Found it at index %d" % someList.index( alist )
        # if you know it will only occur once you might say:
        break

HTH,
Daniel



More information about the Python-list mailing list