Click for All Topics
ValueError
when the passed element does not exist in the list.
Syntax –
list.index(element, start, end)
Parameters –
Example 1-
my_list=[1,11,111,1111,11111,11111]
index_of_11=my_list.index(11)
print("Index of 11:",index_of_11)
# Output -
# Index of 11: 1
11
by using the .index()
method and store it in the variable index_of_11
.
Example 2- find the index of an element using the start and end parameter
my_list=['Hello', 'Student', 'this', 'is', 'edSlash']
index_of_is=my_list.index('is',1,4)
print("Index Position of 'is' :",index_of_is)
# Output -
# Index Position of 'is' : 3
Example 3- find the index of an element that doesn’t exist in the list
my_list=['a','b','c','d','e','f']
print(my_list.index('g'))
# Output -
# Traceback (most recent call last):
# File "/home/main.py", line 2, in
# print(my_list.index('g'))
# ValueError: 'g' is not in list
Example 4- find the index value of an element without using any in-built(.index()
) method
lst=[10,20,30,40,50,60,10,70,20]
for i in range(len(lst)):
if lst[i]==60:
print(i)
break
# Output -
# Index value of 60: 5
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India