■ Before comprehension
words = ["apple", "bus", "camera", "dog", "eye", "five", "game"]
new_list = []
for x in words:
if "e" in x:
new_list.append(x)
print(new_list)
Output
['apple', 'camera', 'eye', 'five', 'game']
■ After comprehension
words = ["apple", "bus", "camera", "dog", "eye", "five", "game"]
new_list = [x for x in words if "e" in x]
print(new_list)
['apple', 'camera', 'eye', 'five', 'game']
No comments:
Post a Comment