Comprehensions

Posted: less than 1 minute read

List

squares = [i * i for i in range(10)]
sentence = 'the rocket came back from mars'
vowels = [i for i in sentence if i in 'aeiou']

def is_consonant(letter):
    vowels = 'aeiou'
    return letter.isalpha() and letter.lower() not in vowels
consonants = [i for i in sentence if is_consonant(i)]
original_prices = [1.25, -9.45, 10.22, 3.78, -5.92, 1.16]
prices = [i if i > 0 else 0 for i in original_prices]

Set

quote = "life, uh, finds a way"
unique_vowels = {i for i in quote if i in 'aeiou'}

Dictionary

squares = {i: i * i for i in range(10)}
squares

Leave a comment