Answers for "how to get index of element in list"

3

get all index of item in list python

indices = [i for i, x in enumerate(my_list) if x == "whatever"]
Posted by: Guest on March-11-2020
43

python find index by value

>>> ["foo", "bar", "baz"].index("bar")
1
Posted by: Guest on November-19-2019
1

find index of sublist in list python

greeting = ['hello','my','name','is','bob','how','are','you','my','name','is']

def find_sub_list(sl,l):
    results=[]
    sll=len(sl)
    for ind in (i for i,e in enumerate(l) if e==sl[0]):
        if l[ind:ind+sll]==sl:
            results.append((ind,ind+sll-1))

    return results

print find_sub_list(['my','name','is'], greeting) 
# [(1, 3), (8, 10)]
Posted by: Guest on October-13-2020
8

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])
Posted by: Guest on January-02-2020
19

python index method

my_string = "Hello World!"

my_string.index("l") # outputs 2
# this method only outputs the index of the first "l" value in the string/list
Posted by: Guest on June-07-2020
0

how to get index of element in list

# Python3 program for demonstration  
# of list index() method
  
list1 = [1, 2, 3, 4, 1, 1, 1, 4, 5]
  
# Will print the index of '4' in list1
print(list1.index(4))
  
list2 = ['cat', 'bat', 'mat', 'cat', 'pet']
  
# Will print the index of 'cat' in list2 
print(list2.index('cat'))
Posted by: Guest on November-24-2021

Code answers related to "how to get index of element in list"

Python Answers by Framework

Browse Popular Code Answers by Language