Python List Index() - Programiz
Maybe your like
Become a certified Python programmer.
ENROLLPopular Tutorials
Getting Started With Python Python if Statement while Loop in Python Python Lists Dictionaries in Python Start Learning PythonPopular Examples
Add two numbers Check prime number Find the factorial of a number Print the Fibonacci sequence Check leap year Explore Python ExamplesReference Materials
Built-in Functions List Methods Dictionary Methods String Methods View allCreated with over a decade of experience.
- Learn
- Practice
- Compete
Certification Courses
Created with over a decade of experience and thousands of feedback.
Learn Python Learn HTML Learn JavaScript Learn SQL Learn DSA View all Courses onBecome a certified Python programmer.
Try Programiz PRO!Popular Tutorials
Getting Started With Python Python if Statement while Loop in Python Python Lists Dictionaries in Python Start Learning Python All Python TutorialsReference Materials
Built-in Functions List Methods Dictionary Methods String Methods View all Python JavaScript C C++ Java R KotlinBecome a certified Python programmer.
Try Programiz PRO!Popular Examples
Add two numbers Check prime number Find the factorial of a number Print the Fibonacci sequence Check leap year All Python ExamplesPython List Methods
- Python List index()
- Python List append()
- Python List extend()
- Python List insert()
- Python List remove()
- Python List count()
- Python List pop()
- Python List reverse()
- Python List sort()
- Python List copy()
- Python List clear()
Python Tutorials
- Python Tuple index()
- Python String index()
- Python List insert()
- Python List pop()
- Python List
- Python String rindex()
Python List index() The index() method returns the index of the specified element in the list.
Example
animals = ['cat', 'dog', 'rabbit', 'horse'] # get the index of 'dog' index = animals.index('dog') print(index) # Output: 1Syntax of List index()
The syntax of the list index() method is:
list.index(element, start, end)list index() parameters
The list index() method can take a maximum of three arguments:
- element - the element to be searched
- start (optional) - start searching from this index
- end (optional) - search the element up to this index
Return Value from List index()
- The index() method returns the index of the given element in the list.
- If the element is not found, a ValueError exception is raised.
Note: The index() method only returns the first occurrence of the matching element.
Example 1: Find the index of the element
# 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)Output
The index of e: 1 The index of i: 2Example 2: Index of the Element not Present in the List
# vowels list vowels = ['a', 'e', 'i', 'o', 'u'] # index of 'p' is vowels index = vowels.index('p') print('The index of p:', index)Output
ValueError: 'p' is not in listExample 3: Working of index() With Start and End Parameters
# alphabets list alphabets = ['a', 'e', 'i', 'o', 'g', 'l', 'i', 'u'] # index of 'i' in alphabets index = alphabets.index('e') # 1 print('The index of e:', 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)Output
The index of e: 1 The index of i: 6 Traceback (most recent call last): File "*lt;string>", line 13, in ValueError: 'i' is not in listAlso Read: Python Program to Access Index of a List Using for Loop
Before we wrap up, let’s put your knowledge of Python list index() to the test! Can you solve the following challenge?
Challenge:Write a function to find the index of a given element in a list.
- For example, with inputs [1, 2, 3, 4, 5] and 3, the output should be 2.
Sorry about that.
How can we improve it? Feedback * Leave this field blankYour builder path starts here. Builders don't just know how to code, they create solutions that matter.
Escape tutorial hell and ship real projects.
Try Programiz PRO- Real-World Projects
- On-Demand Learning
- AI Mentor
- Builder Community
Python References
Python Library
Python List insert()
Python Library
Python List pop()
Python Library
Python List count()
Python Library
Python List remove()
Tag » How To Print A List Python
-
Print Lists In Python (5 Different Ways) - GeeksforGeeks
-
How To Print A List In Python? - FavTutor
-
3 Easy Methods To Print A Python List - AskPython
-
Python List Print - 7 Different Ways To Print A List You Must Know
-
How To Print A List In Python? | Flexiple Tutorials
-
How To Print A List In Python - Codingem
-
How To "properly" Print A List? - Python - Stack Overflow
-
Python List Print - Initial Commit
-
Python Print List Example - HowToDoInJava
-
Python: Generate And Print A List Of First And Last 5 Elements Where ...
-
Create And Print Lists Using Python
-
Python Tips - How To Easily Convert A List To A String For Display
-
Python List Reverse() - Programiz
-
6 Ways To Print Lists Of List In Python