This article shows how to find element in list.
Contents
- How to find an element by exact match and check if it is included in the list
- How to find an element by exact match and get index
- How to find elements by exact match and get the number of elements
- How to find an element of list by partial match
sponsored link
How to find an element by exact match and check if it is included in the list
To check if an element is included in list, the " in operator " is used.
If list contains an element, True is returned.
How to write
Value in List
Here is an example code.
list_1 = ["Apple", "Banana", "Orange", "Bana", "Banana"]
print("App" in list_1)
# False
print("Apple" in list_1)
# True
Using "in operator", an element is searched by exact match.
sponsored link
How to find an element by exact match and get index
To find an element by exact match and get index, the " index method " is used.
How to write
List.index( Value )
Here is an example code.
list_1 = ["Apple", "Banana", "Orange", "Bana", "Banana"]
print(list_1.index("Banana"))
# 1
As a supplement, here are two examples of the use of "index method".
How to avoid error stop when using "index method"
If a value not included in the list is specified, an error stop occurs.
list_1 = ["Apple", "Banana", "Orange", "Bana", "Banana"]
print(list_1.index("Ban"))
# 'Ban' is not in list
You can avoid the error stop by using the "in operator" as in the following code.
list_1 = ["Apple", "Banana", "Orange", "Bana", "Banana"]
search_value = "Ban"
if search_value in list_1 :
print(list_1.index(search_value))
else :
print("None")
# None
How to get the index of a duplicate element using the index method
The "index method" returns only the index of the first element if the list contains duplicate elements.
list_1 = ["Apple", "Banana", "Orange", "Bana", "Banana"]
print(list_1.index("Banana"))
# 1
In this sample code, there are two "bananas" in the list, but only the index "1" of the first element is returned.
Multiple indexes can be obtained by using "list comprehension" and "enumerate function".
◆List Comprehension
Here is how to write the list comprehension.
List Comprehension
[ Formula for Variable in List If-Statement ]
Extracts elements from the list for which " if statement" is True and returns a new list.
◆enumerate function
By using "enumerate function", the index and element are simultaneously extracted from a list.
Here is an example code to get the indexes of all duplicate elements in a list.
list_1 = ["Apple", "Banana", "Orange", "Bana", "Banana"]
print([i for i, x in enumerate(list_1) if x == 'Banana'])
# [1, 4]
sponsored link
How to find elements by exact match and get the number of elements
To find elements by exact match and get the number of elements, the " count method " is used.
How to write
List.count( Value )
Here is an example code.
list_1 = ["Apple", "Banana", "Orange", "Bana", "Banana"]
print(list_1.count("Apple"))
# 1
print(list_1.count("Ban"))
# 0
sponsored link
How to find an element of list by partial match
I show how to find an element of list by partial match.
Here is an example code.
list_1 = ["Apple", "Banana", "Orange", "Bana", "Banana"]
search_value = "Ban"
index_list = []
for i, e in enumerate(list_1) :
if search_value in e :
index_list.append(i)
print(index_list)
# [1, 3, 4]
A example code using list comprehension.
list_1 = ["Apple", "Banana", "Orange", "Bana", "Banana"]
search_value = "Ban"
print([i for i, x in enumerate(list_1) if search_value in x])
# [1, 3, 4]
sponsored link