In this tutorial, we will learn how to check if a word is present in a list in Python. We will explore different solutions to achieve this goal, including the use of simple conditional statements, the built-in in
keyword, and list comprehensions. By the end of this tutorial, you will be able to efficiently check for the existence of a word within a list using Python.
Step 1: Using a simple conditional statement
A simple approach to check if a word exists in a list is to loop through the list and have a conditional statement to check whether the word is the same as the list element.
1 2 3 4 5 6 7 8 9 |
word_list = ["apple", "banana", "cherry", "orange"] word_to_find = "cherry" for word in word_list: if word == word_to_find: print("The word is in the list.") break else: print("The word is not in the list.") |
Output:
The word is in the list.
Step 2: Using the “in” keyword
Python has a built-in keyword called in
that can be used to check if an element is present in a list. The in
keyword returns True
if the word is in the list, and False
otherwise. This method offers a more concise approach than using a loop.
1 2 3 4 5 6 7 |
word_list = ["apple", "banana", "cherry", "orange"] word_to_find = "cherry" if word_to_find in word_list: print("The word is in the list.") else: print("The word is not in the list.") |
Output:
The word is in the list.
Step 3: Using list comprehensions
A more advanced approach to check for the presence of a word in a list is to use Python’s list comprehensions. This method involves creating a new list that contains all occurrences of the word in the list and then checking the length of the newly created list.
1 2 3 4 5 6 7 8 9 |
word_list = ["apple", "banana", "cherry", "orange"] word_to_find = "cherry" found_words = [word for word in word_list if word == word_to_find] if len(found_words) > 0: print("The word is in the list.") else: print("The word is not in the list.") |
Output:
The word is in the list.
Full code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# Method 1: Using a simple conditional statement word_list = ["apple", "banana", "cherry", "orange"] word_to_find = "cherry" for word in word_list: if word == word_to_find: print("The word is in the list.") break else: print("The word is not in the list.") # Method 2: Using the "in" keyword if word_to_find in word_list: print("The word is in the list.") else: print("The word is not in the list.") # Method 3: Using list comprehensions found_words = [word for word in word_list if word == word_to_find] if len(found_words) > 0: print("The word is in the list.") else: print("The word is not in the list.") |
Conclusion
In this tutorial, we have learned how to check if a word is present in a list using three different methods in Python: a simple conditional statement, the in
keyword, and list comprehensions. The most concise and efficient method among these is using the in
keyword. However, you can choose any method according to your requirements and preferences.