index of list of lists

Ivan Illarionov ivan.illarionov at gmail.com
Thu Apr 17 22:48:19 CEST 2008


On Thu, 17 Apr 2008 05:15:52 +0300, Daniel NL wrote:

> 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?

You probably need something like this:
[x for x, y, z in someList if x == 'somestring']

or this:
for x, y, z in someList:
    if x == 'somestring':
        return x

-- 
Ivan



More information about the Python-list mailing list