The list is an important container in python as it stores elements of all the data types as a collection. Knowledge of certain list operations is necessary for day-day programming. This article discusses the Fastest way to check if a value exists in a list or not using Python.
Example:
list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True
Input: 7 # Check if 7 exist or not. Output: False
Method 1: Naive Method
In the Naive method, one easily uses a loop that iterates through all the elements to check the existence of the target element. This is the simplest way to check the existence of the element in the list. Python is the most conventional way to check if an element exists in a list or not. This particular way returns True if an element exists in the list and False if the element does not exist in the list. The list need not be sorted to practice this approach of checking.
Example 1: Check if an element exists in the list using the if-else statement
Python3
lst
=
[
1
,
6
,
3
,
5
,
3
,
4
]
i
=
7
if
i
in
lst:
print
[
"exist"
]
else
:
print
[
"not exist"
]
Output:
not exist
Example 2: Check if an element exists in the list using a loop
Python3
test_list
=
[
1
,
6
,
3
,
5
,
3
,
4
]
for
i
in
test_list:
if
[i
=
=
4
]:
print
[
"Element Exists"
]
Output:
Element Exists
Example 3: Check if an element exists in the list using “in”
Python3
test_list
=
[
1
,
6
,
3
,
5
,
3
,
4
]
if
[
4
in
test_list]:
print
[
"Element Exists"
]
Output:
Element Exists
Example 4: Check if an element exists in the list using any[] function
Python3
test_list
=
[
1
,
6
,
3
,
5
,
3
,
4
]
result
=
any
[item
in
test_list
for
item
in
test_list]
print
[
"Does string contain any list element : "
+
str
[
bool
[result]]]
Output:
Does string contain any list element : True
Method 2: Check if an element exists in the list using count[]
We can use the in-built python List method, count[], to check if the passed element exists in the List. If the passed element exists in the List, the count[] method will show the number of times it occurs in the entire list. If it is a non-zero positive number, it means an element exists in the List. Demonstrating to check the existence of elements in the list using count[].
Python3
test_list
=
[
10
,
15
,
20
,
7
,
46
,
2808
]
print
[
"Checking if 15 exists in list"
]
exist_count
=
test_list.count[
15
]
if
exist_count >
0
:
print
[
"Yes, 15 exists in list"
]
else
:
print
[
"No, 15 does not exists in list"
]
Output:
Checking if 15 exists in list Yes, 15 exists in list
Method 3: Check if an element exists in the list using sort + bicect_left + set
Converting the list into the set and then using it can possibly be more efficient than only using it. But having efficiency for a plus also has certain negatives. One among them is that the order of the list is not preserved, and if you opt to take a new list for it, you would require to use extra space. Another drawback is that set disallows duplicity and hence duplicate elements would be removed from the original list. In the conventional binary search way of testing element existence, hence list has to be sorted first and hence does not preserve the element ordering. bisect_left[] returns the first occurrence of the element to be found and has worked similarly to lower_bound[] in C++ STL.
Note: The bisect function will only state the position of where to insert the element but not the details about if the element is present or not.
Demonstrating to check existence of element in list using set[] + in and sort[] + bisect_left[]
Python3
from
bisect
import
bisect_left ,bisect
test_list_set
=
[
1
,
6
,
3
,
5
,
3
,
4
]
test_list_bisect
=
[
1
,
6
,
3
,
5
,
3
,
4
]
print
[
"Checking if 4 exists in list [ using set[] + in] : "
]
test_list_set
=
set
[test_list_set]
if
4
in
test_list_set :
print
[
"Element Exists"
]
print
[
"Checking if 4 exists in list [ using sort[] + bisect_left[] ] : "
]
test_list_bisect.sort[]
if
bisect_left[test_list_bisect,
4
]!
=
bisect[test_list_bisect,
4
]:
print
[
"Element Exists"
]
else
:
print
[
"Element doesnt exist"
]
Output:
Checking if 4 exists in list [ using set[] + in] : Element Exists Checking if 4 exists in list [ using sort[] + bisect_left[] ] : Element Exists
Method 4: Using find[] method
Python3
test_list
=
[
10
,
15
,
20
,
7
,
46
,
2808
]
print
[
"Checking if 15 exists in list"
]
x
=
list
[
map
[
str
,test_list]]
y
=
"-"
.join[x]
if
y.find[
"15"
] !
=
-
1
:
print
[
"Yes, 15 exists in list"
]
else
:
print
[
"No, 15 does not exists in list"
]
Output
Checking if 15 exists in list Yes, 15 exists in list
Method 5: Using Counter[] function
Below is the implementation:
Python3
from
collections
import
Counter
test_list
=
[
10
,
15
,
20
,
7
,
46
,
2808
]
frequency
=
Counter[test_list]
if
[frequency[
15
] >
0
]:
print
[
"Yes, 15 exists in list"
]
else
:
print
[
"No, 15 does not exists in list"
]
Output
Yes, 15 exists in list