Skip to content

Commit 61ddb87

Browse files
committed
TRY THIS: COMPREHENSIONS
1 parent 6a599d4 commit 61ddb87

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

Chapter8/8_4_1.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
Write the code to create a dictionary of the numbers and their cubes from 11
77
through 15."""
88

9-
"""x = [1, 3, 5, 0, -1, 3, -2]
10-
x_remove = [ x.remove(i) for i in x if i < 0]
11-
print(x)"""
12-
#output: [1, 3, 5, 0, 3]
9+
x = [1, 3, 5, 0, -1, -3, -2]
10+
x_remove = [i for i in x if i >= 0]
11+
print(x_remove)
12+
#output: [1, 3, 5, 0]
1313

14-
"""for i in range(1,101):
15-
if i % 2 != 0:
16-
print(i)"""
17-
#output: 1 3 5 7
14+
odd = ( i for i in range(1,101) if i % 2 !=0)
15+
for i in odd:
16+
print(i,)
17+
#output: 1 3 5 7 ...
1818

1919
cubs = {i: i**3 for i in range(11,16)}
2020
print(cubs)

0 commit comments

Comments
 (0)