Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all indexes for a python list [duplicate]

Tags:

python

I can get the first index by doing:

l = [1,2,3,1,1]
l.index(1) = 0

How would I get a list of all the indexes?

l.indexes(1) = [0,3,4]

?

like image 368
David542 Avatar asked Jan 28 '15 00:01

David542


People also ask

How do I find the index of duplicates in a list?

Method #1 : Using loop + set() In this, we just insert all the elements in set and then compare each element's existence in actual list. If it's the second occurrence or more, then index is added in result list.

Can Python index have duplicates?

duplicated() function Indicate duplicate index values. Duplicated values are indicated as True values in the resulting array. Either all duplicates, all except the first, or all except the last occurrence of duplicates can be indicated. The value or values in a set of duplicates to mark as missing.


1 Answers

>>> l = [1,2,3,1,1]
>>> [index for index, value in enumerate(l) if value == 1]
[0, 3, 4]
like image 153
Cory Kramer Avatar answered Oct 21 '22 00:10

Cory Kramer