Check if an item exists in a list in Python

You have a list, and you want to check whether an element you have is already present inside the list. There are a few ways you can do it in Python.

A quick way to check if an item is inside a list

  1. Create a variable with the value you want to check.
  1. Create a list of items.
  1. Use the IF condition to check the value.
  1. Use ELSE to execute a message if the value is not present in the list.

The full example is going to look like this:

Alternative version with the list.count() function

If you want to check not only whether the value exists in a list, but also how many times, you can use the list.count function.

Add value if unique in a list

In the next example, we are going to create a function that checks whether the value is not present inside a list.

If the value is present, then it means that the value is not unique and the function returns False, otherwise the value is unique, and function adds it to the list and returns True.