Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - Check if given words appear together in a list of sentence
When working with lists of sentences, we often need to check if specific words appear together in any sentence. Python provides several approaches to solve this problem using loops, built-in functions, and functional programming techniques.
Using List Comprehension with len()
This approach uses list comprehension to find matching words and checks if all target words are present ?
sentences = ['Eggs on Sunday', 'Fruits on Monday', 'Eggs and Fruits on Wednesday']
target_words = ['Eggs', 'Fruits']
print("Given list of sentences:")
print(sentences)
print("Given list of words:")
print(target_words)
result = []
for sentence in sentences:
matching_words = [word for word in target_words if word in sentence]
if len(matching_words) == len(target_words):
result.append(sentence)
print("Sentences containing all target words:")
print(result)
Given list of sentences: ['Eggs on Sunday', 'Fruits on Monday', 'Eggs and Fruits on Wednesday'] Given list of words: ['Eggs', 'Fruits'] Sentences containing all target words: ['Eggs and Fruits on Wednesday']
Using the all() Function
The all() function provides a cleaner approach by checking if all words exist in each sentence ?
sentences = ['Eggs on Sunday', 'Fruits on Monday', 'Eggs and Fruits on Wednesday']
target_words = ['Eggs', 'Fruits']
print("Given list of sentences:")
print(sentences)
print("Given list of words:")
print(target_words)
# Check which sentences contain all words
contains_all = [all([word in sentence for word in target_words]) for sentence in sentences]
print("Sentences containing all target words:")
result = [sentences[i] for i in range(len(contains_all)) if contains_all[i]]
print(result)
Given list of sentences: ['Eggs on Sunday', 'Fruits on Monday', 'Eggs and Fruits on Wednesday'] Given list of words: ['Eggs', 'Fruits'] Sentences containing all target words: ['Eggs and Fruits on Wednesday']
Using lambda and map() Functions
This functional programming approach uses split() to match whole words and combines lambda with map() ?
sentences = ['Eggs on Sunday', 'Fruits on Monday', 'Eggs and Fruits on Wednesday']
target_words = ['Eggs', 'Fruits']
print("Given list of sentences:")
print(sentences)
print("Given list of words:")
print(target_words)
# Use lambda and map to check word presence
contains_all = list(map(lambda sentence: all(map(lambda word: word in sentence.split(),
target_words)), sentences))
print("Sentences containing all target words:")
result = [sentences[i] for i in range(len(contains_all)) if contains_all[i]]
print(result)
Given list of sentences: ['Eggs on Sunday', 'Fruits on Monday', 'Eggs and Fruits on Wednesday'] Given list of words: ['Eggs', 'Fruits'] Sentences containing all target words: ['Eggs and Fruits on Wednesday']
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| List comprehension with len() | Good | Good | Beginners, clear logic |
| all() function | Excellent | Best | Most situations |
| lambda and map() | Fair | Good | Functional programming style |
Conclusion
The all() function approach provides the cleanest and most efficient solution for checking if words appear together in sentences. Use split() when you need exact word matching rather than substring matching.
