If you have a list in Python and you want to search to see if a particular element is present in the list, you can make use of the one of the following three ways,
1. Using the "in" and "not in" operator
Example:list_of_cities = ["NYC", "Tokyo", "Chicago", "Austin", "London"]
if "London" in list_of_cities:
print(f"London is the list {list_of_cities}")
if "Mumbai" not in list_of_cities:
print(f"Mumbai is not the list {list_of_cities}")
Output:
London is the list ['NYC', 'Tokyo', 'Chicago', 'Austin', 'London']
Mumbai is not the list ['NYC', 'Tokyo', 'Chicago', 'Austin', 'London']
2. Using the index() method
Example:my_list = ["Apple", "Mango", "Banana", "Grapes"]
try:
index = my_list.index("Mango")
print("Mango is at index", index)
except ValueError:
print("Mango is not in the list")
Mango is at index 1
Its better to handle ValueError as if the element you are looking for is not found you get this error.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-42-3b37ee759fe2> in <cell line: 3>()
1 my_list = ["Apple", "Mango", "Banana", "Grapes"]
2
----> 3 index = my_list.index("Orange")
4 print("Mango is at index", index)
ValueError: 'Orange' is not in list
3. Using the count() method
Example:my_list = [24,21,12,45,24,12,45,23]
if my_list.count(10) > 0:
print("10 is found in my_list")
else:
print("10 is not found in my_list")
10 is not found in my_list
HashTags: #searchAListPython #pythonFindInList #listSearchPython #pythonFindElementInList #pythonFindItemInList
References:
- https://docs.python.org/3/library/array.html?highlight=index#array.array.index- https://docs.python.org/3/library/array.html?highlight=index#array.array.count
You can download this article in various formats for your convenience. Choose from the options below:
Facing issues? Have Questions? Post them here! I am happy to answer!
- Python List of Lists with Examples
- Fix: ERROR: Could not find a version that satisfies the requirement tkinter (from versions: none) ERROR: No matching distribution found for tkinter
- How to Convert Python Notebook .ipynb (JSON) to Executable .py Module
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- Python: Pandas Merge Indicator (Left, Right and Both) Example
- Fix: EOFError Exception in Python
- How to URL Decode a Query String in Python
- How to take user input from the console in a Python program
- Compare two lists in Python and return matches
- How to change the Python Default version
- Fix: KeyError: exception in Python
- How to get the Execution Time of A Python Program
- Fix: ModuleNotFoundError: No module named boto3 [Python]
- How to get unique values from a list in Python
- How to pip install Python Modules in VSCode
- Python: Fix - TypeError: NoneType object is not iterable
- How to delete a file using Python code example
- How to create a dictionary comprehension in Python
- Python: Get just the filename without extension using Path
- How to comment out a block of code in Python
- How to add two float numbers in Python
- Python: How to POST Json Data with HTTP Request
- Get MD5 Hash as Checksum of a String in Python
- Python - Convert float to String
- How to Parse XML String in Python
- Closest Alternate to Notepad on Mac - MacOS
- How to detect Operating System using Java code - Java
- [fix] Loading class com.mysql.jdbc.Driver is deprecated - MySQL
- How to find version of Cargo in Rust - Rust
- How to Show Commit Date in Eclipse Git History Details - Git
- Install Docker on Mac using brew cask - Docker
- How to Create a File using PowerShell on Mac - Powershell
- Use Netbeans keyboard shortcuts in Android Studio - Android-Studio