Let’s appreciate how economical list comprehensions are. The following code stores words that contain the letter “o”, in a list:
words_with_o = []
word_collection = ['Python', 'Like', 'You', 'Mean', 'It']
for word in word_collection:
if "o" in word.lower():
words_with_o.append(word)
in a single line, using a list comprehension:
word_collection = ['Python', 'Like', 'You', 'Mean', 'It']
result = [w for w in word_collection if 'o' in w.lower()]
Nested list comprehensions
# This creates a 3x4 "matrix" (list of lists) of zeros. >>> [[0 for col in range(4)] for row in range(3)] [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]




















