Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I just need to find all the occurrences of an element in a list.

Because, if we use index(), that will give you the first occurrence if an item, in that list. Now, what if I want to return all the indices in a list?

would anyone please help me with this? 

Thank you !!

1 Answer

0 votes
by (26.4k points)
edited by

You can find all the occurrences of an element in a list by using

List Comprehension

indices = [i for i, x in enumerate(my_list) if x == "whatever"]

Come & join our python online course and become a Python expert

To know more about this topic, you can also look at the following video tutorial :

Related questions

0 votes
1 answer
asked Jul 22, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
+2 votes
3 answers
+3 votes
2 answers
0 votes
1 answer

Browse Categories

...