/  Technology   /  Fastest way to check if a value exists in a list in Python
Fastest way to check if a value exists in a list in Python

Fastest way to check if a value exists in a list in Python

 

Before jumping into knowing which is the fastest way to check if a value exists or not in a list, let’s first find different ways to do this by discussing each of the below methods in detail. 

  • Using in operator
  • Using count()
  • Using any()
  • Using list comprehension

 

1. Using in operator:

Syntax:

item in list

It returns a Boolean value, True if it is found, else False.

 

We have taken a list named demo_list with the following elements.

Example:

demo_list= ['Red', 'Orange', 'Black']

Output:

 

Now using the in operator, we’ll check whether ‘Orange’ is present in the above list or not.

Example:

demo_list= ['Red', 'Orange', 'Black']

if 'Orange' in demo_list:
   print("Yes! Orange is present in the List ", demo_list)

Output:

 

Let’s see what happens if an item is not present in a list.

Example:

demo_list= ['Red', 'Orange', 'Black']

if 'Yellow' in demo_list:
   print("Yes! Yellow is present in the List ", demo_list)

else:
   print("No! Yellow is not found in the list",demo_list)

Output:

 

2. List Comprehension:

In this method, let’s have a string and a list, and check if the string contains any of the elements in the list. demo_string is our string and demo_list is the list we used in this example. In the list comprehension, the iterator ele is used to iterate over the list and the string. And finally, we’ll print a Boolean result.

 

Example:

demo_string = "White is my favoyrite colour"
demo_list= ['Red', 'orange', 'Black']

print("The original string : " + demo_string)
print("The original list : " + str(demo_list))

result = [ele for ele in demo_list if(ele in demo_string)]
print("Does string contain any list element : " +str(bool(result)))

Output:

 

3. Using list.count():

This function finds the number of occurrences of a given element in the given list. If the returned value is greater than 0, then it denotes the presence of that element in the list. It takes one parameter, the element to be checked.

 

Example:

demo_list= ['Red', 'Orange', 'Black']
if demo_list.count('Red') > 0:
   print("Yes! Red is present in List ", demo_list)

Output:

 

4. Using any():

This function is the most classical and efficient way to perform this task. It checks for a match in a string with a match of each list element. Here also we have a string and a list named demo_string and demo_list and the iterator item iterates over this list and string. And finally print the result as a Boolean value.

 

Example:

demo_string = "Black is my favoyrite colour"
demo_list= ['Red', 'orange', 'Black']

print("The original string : " + demo_string)
print("The original list : " + str(demo_list))

result = any(item in demo_string for item in demo_list)
print("Does string contain any list element : " +str(bool(result)))

Output:

As we have discussed all the methods to check if an item is present in a list or not. The fastest way to do this task is using the in operator

 

Leave a comment