Python: How to Check If Element is or not in List


Let's see some examples to demonstrate how to check If an element is or is not in the List.


Example 1:
numbers = [1, 5, 2, 3, 9]

if 2 in numbers:
    print("2 is present in the list")
else:
    print("2 is not present in the list")

if 6 in numbers:
    print("6 is present in the list")
else:
    print("6 is not present in the list")
Output:
2 is present in the list
6 is not present in the list


Let's see another example with a list of strings

cities = ['London', 'Paris', 'Tokyo', 'New York', 'Sydney']

check_element_1 = 'Paris'
check_element_2 = 'Rome'


if check_element_1 in cities:
    print(f"{check_element_1} is present in the list of cities")
else:
    print("{check_element_1} is not present in the list of cities")

if check_element_2 in cities:
    print(f"{check_element_2} is present in the list of cities")
else:
    print(f"{check_element_2} is not present in the list of cities")
Output:
Paris is present in the list of cities
Rome is not present in the list of cities

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap