Home//

Python Check if String is in List

Python Check if String is in List

Minh Vu

By Minh Vu

Updated Oct 24, 2023

Hey guys, this is Minh Vu again. This time, I will show you different ways to check if a string is in a Python list.

Let's get started right now.

Check if a String is in a Python List

In short, you have 3 ways to check if a string is in a Python list:

Want to know some examples? Let's dive into it.

Using the in Operator

You can use the in operator to check if an element is in a list or not.

An element can be a string, a number, a boolean, or even a list. In this case, we will check if a string is in a list.

The syntax is: if a in b:, which means if element a is in list b, then do something.

text = "WiseCode" text_list = ["WiseCode", "Minh Vu", "Python"] if text in text_list: print(f"{text} is in the list") else: print(f"{text} is not in the list") # Output: # WiseCode is in the list

We have a string text and a list text_list here. Then we use the in operator to check if text is in text_list or not.

In this case, "WiseCode" is in ["WiseCode", "Minh Vu", "Python"], so the output is WiseCode is in the list.

Another case when the string is not in the list:

text = "WiseCode" text_list = ["Minh Vu", "Python"] if text in text_list: print(f"{text} is in the list") else: print(f"{text} is not in the list") # Output: # WiseCode is not in the list

Using the count() Method

If you want to know how many times an element appears in a list, you can use the count() method.

The syntax is: list.count(element), which returns the number of times element appears in list.

So we know that if element is in the list, then it must returns a number greater than 0.

text1 = "WiseCode" text2 = "Minh Vu" text3 = "JavaScript" text_list = ["WiseCode", "Minh Vu", "Python", "Minh Vu"] count1 = text_list.count(text1) count2 = text_list.count(text2) count3 = text_list.count(text3) if count1 > 0: print(f"{text1} appears {count1} time(s) in the list") else: print(f"{text1} is not in the list") # Output: # WiseCode appears 1 time(s) in the list if count2 > 0: print(f"{text2} appears {count2} time(s) in the list") else: print(f"{text2} is not in the list") # Output: # Minh Vu appears 2 time(s) in the list if count3 > 0: print(f"{text3} appears {count3} time(s) in the list") else: print(f"{text3} is not in the list") # Output: # JavaScript is not in the list

Cool, we have gone so far. Let's move on to the last method.

Using the index() Method

The index() method returns the index of the first element that matches the specified value.

This method is helpful when you want to find the index of an element in a list.

The syntax is: list.index(element), which returns the index of the first occurrence of element in list, or raises a ValueError if it is not found.

text1 = "WiseCode" text2 = "JavaScript" text_list = ["WiseCode", "Minh Vu", "Python"] try: index1 = text_list.index(text1) print(f"{text1} is in the list at index {index1}") except ValueError: print(f"{text1} is not in the list") # Output: # WiseCode is in the list at index 0 try: index2 = text_list.index(text2) print(f"{text2} is in the list at index {index2}") except ValueError: print(f"{text2} is not in the list") # Output: # JavaScript is not in the list

Conclusion

Let's recap the tutorial. In this tutorial, we learned 3 ways to check if a string is in a Python list:

  • Using the in operator
  • Using the count() method
  • Using the index() method

Please comment if you have any questions. Thank you for reading!

You can search for other posts at home page.
Minh Vu

Minh Vu

Software Engineer

Hi guys, I'm the author of WiseCode Blog. I mainly work with the Elastic Stack and build AI & Python projects. I also love writing technical articles, hope you guys have good experience reading my blog!