index of element in array python code example
Example 1: python index of element in list
# alphabets list
alphabets = ['a', 'e', 'i', 'o', 'g', 'l', 'i', 'u']
# index of 'i' in alphabets
index = alphabets.index('i') # 2
print('The index of i:', index)
# 'i' after the 4th index is searched
index = alphabets.index('i', 4) # 6
print('The index of i:', index)
# 'i' between 3rd and 5th index is searched
index = alphabets.index('i', 3, 5) # Error!
print('The index of i:', index)
Example 2: get index from element in list python
list.index(element)
Example 3: get index of item in list
list.index(element, start, end)
Example 4: position in array python
a_list = [1, 2, 3]
position_of_three = a_list.index(3)
print(position_of_three)
Example 5: how to index an array in python
arrayName[Index Number]
Example 6: python, list, index function
# vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']
# index of 'e' in vowels
index = vowels.index('e')
print('The index of e:', index)
# element 'i' is searched
# index of the first 'i' is returned
index = vowels.index('i')
print('The index of i:', index)