In this article, we will show how to write a Python program to check if the user-given element exists in a List or not with examples. There are multiple options for this; among them, using in operator, not in, and count is the fastest possible solution, followed by the for loop.
Python Program to Check if the Element Exists in a List using in operator
In the program below, we have declared an integer list and are asking the user to enter any list value. Next, the in operator alongside the if statement will check whether the element exists in a list or not. Here, in operator finds and. Returns the boolean value, and if t is booleanTrue, if condition satisfies, so the value exits.
a = [10, 25, 50, 75, 100]
print(a)
num = int(input("Enter value = "))
if num in a:
print("It exists")
else:
print("It does not exists")
[10, 25, 50, 75, 100]
Enter value = 50
It exists
Using the not in operator
This Python program uses the not in operator to check if the user-given element exists in a List or not. It looks similar to the above example, but instead of checking whether it exists. The not in opertaor checks whether the country name is not in the string list. If it does not, it prints the “It does not exist” message. Otherwise, prints the It exists message.
countries = ["India", "USA", "UK", "Italy", "France", "Germany"]
print(countries)
name = input("Enter the Name = ")
if name not in countries:
print("It does not exists")
else:
print("It exists")
['India', 'USA', 'UK', 'Italy', 'France', 'Germany']
Enter the Name = UK
It exists
Using count() function
The count() method counts the occurrence of a list item. So, if the count value is greater than zero, it exits.
countries = ["India", "USA", "UK", "Italy", "France", "Germany"]
print(countries)
name = input("Enter the Name = ")
if countries.count(name) > 0:
print("It exists")
else:
print("It does not exists")
['India', 'USA', 'UK', 'Italy', 'France', 'Germany']
Enter the Name = USA
It exists
Python Program to Check if the Element Exists in a List using for loop
The for loop in this program will iterate each element in a list, and the if statement will compare each item against the given number. If the if statement returns Boolean True, the number was found; Otherwise, it does not exist.
a = [10, 25, 50, 75, 100]
print(a)
num = int(input("Enter value = "))
flag = 0
for value in a:
if value == num:
flag = 1
break
if flag == 1:
print("It exists")
else:
print("It does not exists")
[10, 25, 50, 75, 100]
Enter value = 100
It exists
If we tweak the Python program a bit, apart from checking the item that exists in a list, we can print the element index position. The program below uses the index() function to get the index value.
a = [10, 25, 50, 75, 100]
print(a)
num = int(input("Enter value = "))
flag = i = 0
for value in a:
if value == num:
i = a.index(value)
flag = 1
break
if flag == 1:
print("It exists at Index Position: ", i)
else:
print("It does not exists")
You can try the enumerator to get the above result.
a = [10, 25, 50, 75, 100]
print(a)
num = int(input("Enter value = "))
flag = 0
for i, n in enumerate(a):
if n == num:
flag = 1
break
if flag == 1:
print("It exists at Index Position: ", i)
else:
print("It does not exists")
[10, 25, 50, 75, 100]
Enter value = 100
It exists at Index Position: 4
Python Program to Check if the Element Exists in a List using the index function
The index() function will search for the given element and return the index position of the first occurring value. If it doesn’t find the value, it will raise a ValueError. So, we used the try and except block and utilized the ValueError to check if the list item exists or not.
countries = ["India", "USA", "UK", "Italy", "France", "Germany"]
print(countries)
name = input("Enter the Name = ")
try:
i = countries.index(name)
print("It exists at the Index Position: ", i)
except ValueError:
print("It does not exists")
['India', 'USA', 'UK', 'Italy', 'France', 'Germany']
Enter the Name = Italy
It exists at the Index Position: 3
Using Counter()function
Within the collections library, there is a Counter() function to count the number of occurrences of each item. So, in this Python program, we used the Counter() function to determine or check the existence of the list element. Here, if the count values greater than zero, the item exist in a list; Otherwise, it doesn’t exists.
from collections import Counter
a = [10, 25, 50, 75, 100]
print(a)
num = int(input("Enter value = "))
count = Counter(a)
if count[num] > 0:
print("It exists")
else:
print("It does not exists")
[10, 25, 50, 75, 100]
Enter value = 10
It exists
Using sort() and bisect_left()
In this program, we used three functions, and to use the bisect function, we had to import the library. The sort() function will sort the list items in ascending order. Next, the binary search method bisect_left returns the very first instance of the given element if it is found.
from bisect import bisect, bisect_left
a = [10, 100, 175, 25, 90, 50, 75]
a.sort()
print(a)
num = int(input("Enter value = "))
if bisect_left(a, num) != bisect(a, num):
print("It exists")
else:
print("It does not exists")
[10, 25, 50, 75, 90, 100, 175]
Enter value = 90
It exists
Using lambda and filter() function
This Python program uses the lambda() and the filter() functions to check if the user-given element exists in a List or not. Here, instead of checking, the filter() function will filter the whole list of items and print the one that we are looking for as a new list.
countries = ["India", "USA", "UK", "Italy", "France", "Germany"]
print(countries)
name = input("Enter the Name = ")
new = list(filter(lambda a: name in a, countries))
print(new)
['India', 'USA', 'UK', 'Italy', 'France', 'Germany']
Enter the Name = India
['India']
Using any() function
The any() function will check if the given element matches with any of the last items, and if so, it returns True.
a = [10, 25, 50, 75, 100]
print(a)
num = int(input("Enter value = "))
flag = any(val == num for val in a)
if flag == 1:
print("It exists")
else:
print("It does not exists")
[10, 25, 50, 75, 100]
Enter value = 75
It exists