Check if String Exists in List in Python (3 Examples)


Hello! Welcome to this interesting tutorial in the Python programming language. In this tutorial, I will show you three ways to check whether a character string exists within a Python list.

Here is a quick overview of the three examples:

Let’s dive right into it!


Example 1: Using “in” & “not” Operators

In this first example, we are going to create a Python list of different fruits as shown below.

myFruits = ["mango", "orange", "apple", "peach", "pear", "tangelo"]

Next, we will use Python’s “in” operator to check whether a fruit exists within the list.

if "orange" in myFruits:
  print("Doesn't exist")
# Exists

Via the “if” and “else” statements, our program will return “Exists” because orange does exist within the list. But if it does not exist within the list, then it will return “Doesn’t exist”.

Let us now demonstrate Python’s “not” operator for the string “banana”.

if "banana" not in myFruits:
  print("Doesn't exist")
# Doesn't exist

The program above prints “Doesn’t exist”, if the fruit does not exist within the list; otherwise, it will print out “Exists”.

Example 2: Using For Loop

In this second example, we will use a for loop to check whether a fruit, “orange” in this case, exists within the list of fruits:

for i in myFruits:
  if i == "orange":
# Exists

In the for loop, every string of fruits inside the list is selected iteratively and matched against the fruit string of interest. If there is an accurate match, then the program prints out “Exists”.

Example 3: Using count() Function

In this third example, we will use Python’s built-in count() function, along with the “if” and “else”statement, to check whether a fruit string exists within the list:

if myFruits.count("orange") > 0:
  print("Doesn't exist")
# Exists

The count() function counts the number of times a fruit string appears in the list. Therefore, if you parse a string that doesn’t exist in the list to the function, it will return the value of 0.

In the script above, we used the “if” and “else” statements to check if the count of a fruit string is greater than 0, in which case “Exists” will be printed out; otherwise, “Does not exist” will be printed out.

Video, Further Resources & Summary

Do you need more explanations on how to check if a string exists in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to check if a character string exists in a list in Python.


The YouTube video will be added soon.


Furthermore, I encourage you to check out other interesting Python tutorials on Statistics Globe. You could begin with these:

This post has shown how to check if a string exists in a list in Python. In case you have further questions, you may leave a comment below.


R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.