Python Tutorial - Master Python Programming For Beginners from Scratch

  • Home
  • Getting Started
  • Basics
  • OOP
  • Advanced Python
  • Tkinter
Home » Python Basics » How to Find the Index of an Element in a List

How to Find the Index of an Element in a List

Summary: in this tutorial, you’ll learn how to find the index of an element in a list.

To find the index of an element in a list, you use the index() function.

The following example defines a list of cities and uses the index() method to get the index of the element whose value is 'Mumbai':

cities = ['New York', 'Beijing', 'Cairo', 'Mumbai', 'Mexico'] result = cities.index('Mumbai') print(result)
Code language: Python (python)

It returns 3 as expected.

However, if you attempt to find an element that doesn’t exist in the list using the index() function, you’ll get an error.

This example uses the index() function to find the Osaka city in the cities list:

cities = ['New York', 'Beijing', 'Cairo', 'Mumbai', 'Mexico'] result = cities.index('Osaka') print(result)
Code language: Python (python)

Error:

ValueError: 'Osaka' is not in list
Code language: Python (python)

To fix this issue, you need to use the in operator.

The in operator returns True if a value is in the list. Otherwise, it returns False.

Before using the index() function, you can use the in operator to check if the element that you want to find is in the list. For example:

cities = ['New York', 'Beijing', 'Cairo', 'Mumbai', 'Mexico'] city = 'Osaka' if city in cities: result = cities.index(city) print(f"The {city} has an index of {result}.") else: print(f"{city} doesn't exist in the list.")
Code language: Python (python)

Output:

Osaka doesn't exist in the list.
Code language: Python (python)

Summary

  • Use the in operator with the index() function to find if an element is in a list.
  • Was this tutorial helpful ?
  • YesNo
Previous How to Use a For Loop to Iterate over a List
Next Python Iterables

Getting Started

  • What is Python
  • Install Python
  • Setup VS Code for Python
  • Develop Python Hello World Program

Python Fundamentals

  • Python Syntax
  • Variables
  • Strings
  • Numbers
  • Boolean
  • Constants
  • Comments
  • Type Conversion

Control Flow

  • if…else
  • Ternary Operator
  • for Loop
  • while Loop
  • break
  • continue
  • pass

Functions

  • Python Functions
  • Default Parameters
  • Keyword Arguments
  • Recursive Functions
  • Lambda Expressions
  • Function Docstrings

Python List

  • List
  • Tuple
  • Sorting a List in Place: sort()
  • Sorting a List: sorted()
  • Slicing a List: [::]
  • Unpacking a List
  • Iterating over a List: for loop
  • Finding Index of an Element: index()
  • Iterables
  • Transform List Elements: map()
  • Filtering List Elements: filter()
  • Reducing List Elements: reduce()
  • List Comprehensions

Python Dictionary

  • Dictionary
  • Dictionary Comprehension

Python Set

  • Set
  • Set Comprehension
  • Union of Sets
  • Intersection of Sets
  • Difference between Sets
  • Symmetric Difference of Sets
  • Subset
  • Superset
  • Disjoint Sets

Exception Handling

  • try…except
  • try…except…finally
  • try…except…else

Python Loop with Else Clause

  • for…else
  • while…else
  • do…while Emulation

More on Functions

  • Unpacking Tuples
  • The *args Parameters
  • The **kwargs Parameters
  • Partial Functions

Modules

  • Modules
  • Module Search Path
  • Python __name__
  • Packages

File I/O

  • Reading a Text File
  • Writing to a Text File
  • Creating a Text File
  • Checking If a File Exists
  • Reading from a CSV File
  • Writing to a CSV File

Managing Third-party Packages

  • Python Package Index (PyPI) & PIP
  • Python Virtual Environments
  • Install pipenv on Windows
  • Using pipenv Tool

About pythontutorial.net

Pythontutorial.net helps you master Python programming from scratch fast.

Site Links

  • Home
  • Contact
  • About
  • Privacy Policy

Recent Python Tutorials

  • Tkinter StringVar
  • Tkinter PhotoImage
  • Tkinter OptionMenu
  • Tkinter ScrolledText
  • How to Change the Appearances of Widgets Dynamically Using Ttk Style map() Method

Python References

  • Python String Methods
  • Python MySQL
  • Python PostgreSQL
  • Python Oracle
  • Python SQLite

Copyright © 2021 · by pythontutorial.net. All rights reserved. Log in