Find Items & Indices In A List...

kaerbuhez kaerbuhez at yahoo.fr
Fri Dec 10 10:23:53 EST 2004


<andrea.gavana at agip.it> a écrit dans le message de news: 
mailman.7461.1102691050.5135.python-list at python.org...
> For example, assuming that I have a list like:
>
> mylist = [0, 1, 1, 1, 1, 5, 6, 7, 8, 1, 10]
>
> I would like to find the indices of the elements in the list that are 
> equal
> to 1 (in this case, the 1,2,3,4,9 elements are equal to 1).

List comprehension is your friend:

PythonWin 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on 
win32.
Portions Copyright 1994-2004 Mark Hammond (mhammond at skippinet.com.au) - see 
'Help/About PythonWin' for further copyright information.
>>> mylist = [0, 1, 1, 1, 1, 5, 6, 7, 8, 1, 10]
>>> [i for i, j in enumerate(mylist) if j==1]
[1, 2, 3, 4, 9]
>>> 




More information about the Python-list mailing list