index() to get matching position in list

All list methods

index(x,[start[,end]])

x : input data to check
start: Optional, starting of the search position
end: Optional, End of search position

returns number of matching position.
my_list=['a','b','c','b','a','d']
print("Position of first matching  a : ", my_list.index('a'))
print("Position of first matching  b : ", my_list.index('b'))
print("Position of first matching  d : ", my_list.index('d'))
Output
Position of first matching  a :  0
Position of first matching  b :  1
Position of first matching  d :  5

Using start and end positions

my_list=['a','b','c','b','a','d','a']
print("Position of first matching  a : ", my_list.index('a',2))
print("Position of first matching  b : ", my_list.index('b',1,4))
print("Position of first matching  a : ", my_list.index('a',5))
Output
Position of first matching  a :  4
Position of first matching  b :  1
Position of first matching  a :  6
If not found then ValueError will be raised.
my_list=['a','b','c','b','a','d','a']
print("Position of first matching  b : ", my_list.index('b',5))
This code will give error as b is not available after position 5. Note that first position is 0th in the list.
All list methods Questions with solutions on List pop() to remove item based on position
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer