Find All Indexes Of Element In List Python Code Example


Example 1: get all indices of a value in list python

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

Example 2: get all occurrence indices in list python

a_list = [1, 2, 3, 1]  indices = [] for i in range(len(a_list)):    if a_list[i] == 1:       indices.append(i)  # more concise way a_list = [1, 2, 3, 1] indices = [index for index, element in enumerate(a_list) if element == 1]

Example 3: find an index of an item in a list python

#Example List list = ['apples', 'bannas', 'grapes'] #Use Known Entites In The List To Find The Index Of An Unknown Object Index_Number_For_Bannas = list.index('apples') #Print The Object print(list[Index_Number_For_Bannas])

Example 4: get index of item in list

list.index(element, start, end)

Example 5: python search list of lists for value return index

[(i, colour.index(c))  for i, colour in enumerate(colours)  if c in colour]

Example 6: python index for all matches

indices = list(filter(lambda x: x == 'whatever', my_list))

Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?