list.index() like...but returning lists (for avoiding '0' on multiples hits)

Peter Otten __peter__ at web.de
Mon Dec 22 18:53:34 EST 2003


Gerardo Herzig -Departamento de Proyectos Especiales e Internet- Facultad de
Medicina wrote:

> Hi....i asume this is a veeeery usual question. I'm searching on
> python.org, but their results is not giving me the answer that i looking
> for...So, here i go...when i have myList = [2,4,2,0] ...myList.index(2)
> returns 0 (python 2.2)....There is a built-in function-method that returns
> [0,2]?

I don't think so. But it's not hard to do it on your own:

>>> def indices(iterable, value):
...     return [index for (index, item) in enumerate(iterable) if item ==
value]
...
>>> indices([1,2,3,1,-1], 1)
[0, 3]


Peter




More information about the Python-list mailing list