Saturday, March 24, 2018

List index( ) Method


List index( ) Method

 The index() method returns the lowest index in list that obj appears.
Syntax
list.index(obj)

Parameters
     obj - This is the object to be find out.
Return Value
This method returns index of the found object otherwise raises an exception indicating that the value is not found.

Example
#!/usr/bin/python3

list1 = ['physics', 'chemistry', 'maths']
print ('Index of chemistry', list1.index('chemistry'))
print ('Index of C#', list1.index('C#'))

When we run the above program, it produces the following result-

Index of chemistry 1

Traceback (most recent call last):
     File "test.py", line 3, in
     print ('Index of C#', list1.index('C#'))
        ValueError: 'C#' is not in list




<<< extend ( )                   Go to Index                  insert( ) >>>