List of pythons | indicator()

Python Methods and Functions

Syntax :

 list_name.index (element, start, end) 

Parameters :

  element  - The element whose lowest index will be returned.  start   (Optional)  - The position from where the search begins.  end   (Optional)  - The position from where the search ends. 

Returns :

 Returns lowest index where the element appears. 

Error:

 If any element which is not present is searched, it returns a ValueError 

Code # 1:

# Python3 demo program
# list method ()

  

list1 = [ 1 , 2 , 3 , 4 , 1 , 1 , 1 , 4 , 5 ]

 
# Prints the index & # 39; 4 & # 39; in list1

print (list1.index ( 4 ))

 

list2 = [ 'cat' , 'bat' , 'mat' , ' cat' , 'pet' ]

  
# Prints the index & # 39; cat & # 39; in list2

print (list2.index ( 'cat' ))

Output:

 3 0 

Code # 2:

# Python3 demo
# index () method

  

list1 = [ 1 , 2 , 3 , 4 , 1 , 1 , 1 , 4 , 5 ]

  
# Will print index" 4 "in the sublist
# with index from 4 to 8.

print (list1.index ( 4 , 4 , 8 ) )

 
# Prints index "1" in the sublist
# with index from 1 to 7.

print (list1.index ( 1 , 1 , 7 ))

 

list2 = [ 'cat' , 'bat' , ' mat' , 'cat'

'get' , ' cat' , 'sat' , 'pet' ]

  
# Prints the index & # 39; cat & # 39; in the sublist
# with index from 2 to 6

print (list2.index ( 'cat' , 2 , 6 ))

Output:

 7 4 3 

Code # 3:

# Python3 demo program
# list method ()

 
# A random list that has sublist and tuple

list1 = [ 1 , 2 , 3 , [ 9 , 8 , 7 ], ( 'cat' , 'bat' )]

  
# Prints sublist index [9, 8, 7]

print (list1.index ([ 9 , 8 , 7 ]))

 
# Prints the index of the tuple
# (& # 39; cat & # 39 ;, & # 39; bat & # 39;) inside the list

print (list1.index (( 'cat' , ' bat' )))

Output:

 3 4 

Code # 4: ValueError

# Python3 demo
Index () method error

 

 

list1 = [ 1 , 2 , 3 , 4 , 1 , 1 , 1 , 4 , 5 ]

 
# Return ValueError

print (list1.index ( 10 ))

Output:

 Traceback (most recent call last): File "/home/b910d8dcbc0f4f4b61499668654450d2.py", line 8, in print (list1.index (10)) ValueError: 10 is not in list