List comprehension expressions are embedded in [] as a way to signal that the result is a list. For example:
[num**2 for num in range(10)]
Reading the above statement from right to left, here's what's happening:
range(10) - generate a list
...for num in... iterate through it using num as the iterator variable
num**2 the thing that you're doing to the list. Here, squaring each element.
By including if statements inside list comprehension, you can effectively filter a list for the elements you want (or don't want)
[num**2 for num in range(10) if num %2 ==0]
You can also embed multiple lists inside a list-comprehension expression. This is equivalent to nested loops, but I personally find these confusing.
[first+second for first in ['a','b','c'] for second in ['d','e','f']]
In addition to list comprehension, Python also implements dictionary comprehension. I include an example for completeness, though I find myself using this quite rarely. The example below checks if a phrase is a pangram,
sentence = "Just a simple little sentence the word lengths of which we can count in one line"
wordLengths = {key: len(key) for key in sentence.split(' ')}
[num for num in range(1,1001) if num % 9 ==0]
import random
[random.random() for num in range(100)]
import random
[random.randrange(1,11) for num in range(100)]
Use list comprehension to take sentence and output the words containing more than 3 letters
sentence = 'The Quick Brown Fox Jumped Over The Lazy Dog'
[word for word in sentence.split() if len(word)>3]
Return a list containing all the consonants in sentence. The consonants don't have to be unique, but the list should not have both uppercas and lowercase letters. You should do this in one line of code.
sentence = 'Can you figure out how many CONSONANTS are in this sentence?'
vowels = ['a','e','i','o','u']
[letter for letter in sentence.lower() if (letter not in vowels and letter.isalpha())]
#insert your code here
#Use a dictionary comprehension to count the length of each word in a sentence.
sentence = 'Use a dictionary comprehension to count the length of each word in a sentence'
results = {word:len(word) for word in sentence.split()}
print(results)
Add a line to the code below to get the average word length in sentence
sentence = "Just a simple little sentence the word lengths of which we can count in one line"
wordLengths = {key: len(key) for key in sentence.split(' ')}
sum(wordLengths.values())/float(len(wordLengths.values()))