Skip to content

Commit 0f81139

Browse files
authored
Merge pull request #486 from realpython/list-comprehension-update
Add materials for the list comprehension update
2 parents e68d0c3 + 722a1f5 commit 0f81139

11 files changed

+90
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# When to Use a List Comprehension in Python
2+
3+
This repository holds the code for Real Python's [When to Use a List Comprehension in Python](https://realpython.com/list-comprehension-python) tutorial.
4+
5+
## License
6+
7+
Distributed under the MIT license. See [`LICENSE`](../LICENSE) for more information.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
quote = "life, uh, finds a way"
2+
unique_vowels = {char for char in quote if char in "aeiou"}
3+
print(unique_vowels)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
squares = []
2+
for i in range(10):
3+
squares.append(i * i)
4+
print(squares)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
squares = [i * i for i in range(10)]
2+
print(squares)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import random
2+
3+
4+
def get_weather_data():
5+
return random.randrange(90, 110)
6+
7+
8+
hot_temps = [temp for _ in range(20) if (temp := get_weather_data()) >= 100]
9+
print(hot_temps)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
sentence = "the rocket came back from mars"
2+
vowels = [char for char in sentence if char in "aeiou"]
3+
print(vowels)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
sentence = "The rocket, who was named Ted, came back \
2+
from Mars because he missed his friends."
3+
4+
5+
def is_consonant(letter):
6+
vowels = "aeiou"
7+
return letter.isalpha() and letter.lower() not in vowels
8+
9+
10+
consonants = [char for char in sentence if is_consonant(char)]
11+
print(consonants)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
prices = [1.09, 23.56, 57.84, 4.56, 6.78]
2+
TAX_RATE = 0.08
3+
4+
5+
def get_price_with_tax(price):
6+
return price * (1 + TAX_RATE)
7+
8+
9+
final_prices = [get_price_with_tax(price) for price in prices]
10+
print(final_prices)

python-list-comprehension/map.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
prices = [1.09, 23.56, 57.84, 4.56, 6.78]
2+
TAX_RATE = 0.08
3+
4+
5+
def get_price_with_tax(price):
6+
return price * (1 + TAX_RATE)
7+
8+
9+
final_prices = map(get_price_with_tax, prices)
10+
print(list(final_prices))
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import random
2+
import timeit
3+
4+
TAX_RATE = 0.08
5+
prices = [random.randrange(100) for _ in range(100000)]
6+
7+
8+
def get_price(price):
9+
return price * (1 + TAX_RATE)
10+
11+
12+
def get_prices_with_map():
13+
return list(map(get_price, prices))
14+
15+
16+
def get_prices_with_comprehension():
17+
return [get_price(price) for price in prices]
18+
19+
20+
def get_prices_with_loop():
21+
prices = []
22+
for price in prices:
23+
prices.append(get_price(price))
24+
return prices
25+
26+
27+
print(timeit.timeit(get_prices_with_map, number=100))
28+
print(timeit.timeit(get_prices_with_comprehension, number=100))
29+
print(timeit.timeit(get_prices_with_loop, number=100))

0 commit comments

Comments
 (0)