0

Is there a method to output the position of all of the occurrences of a value in a list?

22nd Jul 2016, 9:56 PM
Kev G
Kev G - avatar
1 Antwort
0
I'm not aware of anything built in that outputs all of the indexes. You can use list_name.index(value) to find the index of the first item matching value in the list. Here's a function that would return the info you want def all_indexes(array, value): out = [] for index, item in enumerate(array): if item == value: out.append(index) return out Or if you'd prefer a lambda function you can use this all_indexes = lambda array, value: [index for index, item in enumerate(array) if item == value] I typed this on my phone, so the spacing may be wrong. Also I'm betting my lambda violated pep8, so i might suggest making it 2 lines
30th Jul 2016, 5:08 AM
Spencer
Spencer - avatar