How to Check if an Item Exists in a List in Python?

Estimated read time 1 min read

In Python, you can check if an item exists in a list using the in operator. The in operator returns a Boolean value of True if the item is present in the list and False if it is not present. Here’s an example:

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

if 3 in my_list:
    print("3 is present in the list")
else:
    print("3 is not present in the list")

Output:

3 is present in the list

In the example above, we first define a list my_list and then use the in operator to check if the value 3 is present in the list. Since 3 is present in the list, the output of the program is "3 is present in the list".

You can also use this technique to check for the existence of a string or any other data type in a list.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply