There are multiple ways to check if an element exists in a Python list, but the easiest and efficient approach is to use the “in” operator. The “in” operator checks whether the list contains a specific element and returns True if the element is found, and False if it is not.
Here is the program to illustrate this:
list = [1, 2, 3, 4, 5] if 3 in list: print("3 found in List : ", list) else: print("The element does not exist in the list") # Output: 3 found in List : [1, 2, 3, 4, 5]
Let’s consider an example where we don’t find an item on the list.
list = [1, 2, 3, 4, 5] if 6 in list: print("3 found in List : ", list) else: print("The list does not contain an element") # Output: The list does not contain an element
The time complexity is O(n), where n is the list length, as it performs a linear search.
What if a list is empty?
If the list is empty, the ‘in’ operator returns False, since there are no elements.
empty_list = [] print(21 in empty_list) # Output: False
Case sensitivity (for strings)
The in operator is case sensitive. So, if the string is the same but the cases are different, it will return False when you check for the existence of the string.
main_list = ["Jaddu", "Washington"] print("jaddu" in main_list) # Output: False
Since we are checking “jaddu” and not “Jaddu”, it can’t find it and returns False.
Nested lists
The in operator checks for the top-level elements; it does not check for nested contents.
nested_list = [[1, 2], [3, 4]] print([1, 2] in nested_list) # Output: True print(1 in nested_list) # Output: False
Alternate approaches
Here are alternate ways:
- Using list comprehension
- Using a list.count()
- Using any()
- Using the not in operator
Using list comprehension
The all() function returns True if all elements of the given iterable evaluate to True. This function can be effectively combined with list comprehension and the “in” operator to check if a list contains all specified elements.
main_list = [1, 2, 3, 4, 5] check_list = [2, 4] if all([item in main_list for item in check_list]): print("The list contains all the elements") else: print("The list does not contain all elements") # Output: The list contains all the elements
Using a list.count()
The count() method returns the number of times the specified element appears in the list. If it’s greater than 0, a given item exists in the list.
list = [1, 2, 3, 4, 5] if list.count(4) > 0: print("4 found in List : ", list) else: print("4 not found in List : ", list) # Output: 4 found in List : [1, 2, 3, 4, 5]
Using any()
The any() function checks if any element in an iterable meets a specified condition. It returns True as soon as it finds an element that satisfies the condition; otherwise, it is False.
data_string = "The last season of Game of Thrones was not good" main_list = ['Stranger Things', 'Squid Game', 'Game of Thrones'] print("The original string : " + data_string) # Output: The original string : The last season of Game of Thrones was not good print("The original list : " + str(main_list)) # Output: The original list : ['Stranger Things', 'Squid Game', 'Game of Thrones'] res = any(item in data_string for item in main_list) print("Does string contain 'Game of Thrones' list element: " + str(res)) # Output: Does string contain 'Game of Thrones' list element: True
Using “not” in operator
The not in operator evaluates to True if the specified element is not in the list, and False otherwise.It negates the in operator’s result.
list = [1, 2, 3, 4, 5] print(7 not in list) # Output: True print(3 not in list) # Output: False
That’s all!
בנימין שמיד
Grate post!
Thank you so much!