Menu

gitpiper

index_of_all python Code Snippet in 2024

listintermediate

Last Updated: 20 April 2024

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) # []

python snippet similar to index_of_all For You in April 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️