how to find an index of an element in a list in python code example

Example 1: get index of list python

list.index(element)

Example 2: get index of item in list

list.index(element, start, end)

Example 3: get index of all element in list python

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

Example 4: how to index python

#you can index in a string like:
string='cupcake'
string[0]#returns 'c'
#you can index in a list like:
list = ['red', 'blue', 'green']
list[0]#returns 'red'
list[0][0]#returns[0] of 'red' wich is r

Example 5: how to find index of list of list in python

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

Example 6: python get element by index

# To return the index of the first occurence of element x in lst
ind = lst.index(x)

Tags:

Misc Example