Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

Python List Contains: Checking If an Item Exists in List

  • 15 May, 2024
  • Com 1
Python List Contains How to Check If Item Exists in List

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.

Visual Representation of in operator

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:

  1. Using list comprehension
  2. Using a list.count()
  3. Using any()
  4. Using the not in operator

Python List Contains - How to Check If Item Exists in List

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.

Using List comprehension

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.

Using the list.count() function

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.

Visual Representation of Using not in operator

list = [1, 2, 3, 4, 5]

print(7 not in list)
# Output: True

print(3 not in list)
# Output: False

That’s all!

Post Views: 636
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Implement Observables in Angular 18
Python certifi: How to Use SSL Certificate

1 Comment

  1. בנימין שמיד

    April 13, 2021 at 6:30 pm

    Grate post!
    Thank you so much!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend