Files
30-seconds-of-code/snippets/index-of-all.md
Angelos Chalaris f6a215e9e3 Kebab file names
2023-04-27 22:00:06 +03:00

538 B

title, tags, cover, firstSeen, lastUpdated
title tags cover firstSeen lastUpdated
All indexes of value list purple-flower-bunch 2020-10-11T13:40:42+03:00 2020-10-11T13:45:19+03:00

Returns a list of indexes of all the occurrences of an element in a list.

  • Use enumerate() and a list comprehension to check each element for equality with value and adding i to the result.
def index_of_all(lst, value):
  return [i for i, x in enumerate(lst) if x == value]
index_of_all([1, 2, 1, 4, 5, 1], 1) # [0, 2, 5]
index_of_all([1, 2, 3, 4], 6) # []