Skip to content

Commit be884b3

Browse files
committedDec 29, 2021
Added task List Comprehension
1 parent b399ac2 commit be884b3

File tree

8 files changed

+80
-1
lines changed

8 files changed

+80
-1
lines changed
 

‎Data structures/Nested Lists/tests/test_task.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111

1212
class TestCase(unittest.TestCase):
13-
def test_loop(self):
13+
def test_list(self):
1414
self.assertEqual('9', output[0], msg='The second printed element should be 9.')
1515
self.assertEqual('10', output[1], msg='The second printed element should be 10.')

‎Loops/List Comprehension/__init__.py

Whitespace-only changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
type: edu
2+
files:
3+
- name: task.py
4+
visible: true
5+
placeholders:
6+
- offset: 194
7+
length: 35
8+
placeholder_text: '''Use list comprehension to build the same list as above'''
9+
- name: tests/test_task.py
10+
visible: false
11+
- name: __init__.py
12+
visible: false
13+
- name: tests/__init__.py
14+
visible: false

‎Loops/List Comprehension/task.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## List Comprehension
2+
3+
You can use a loop to build a list (or another data structure).
4+
For example:
5+
6+
```python
7+
my_list = []
8+
for i in range(5):
9+
my_list.append(i)
10+
11+
print(my_list)
12+
```
13+
Output:
14+
```text
15+
[0, 1, 2, 3, 4]
16+
```
17+
18+
This is nice, but quite bulky. List comprehension offers a more compact syntax when you want to create a new list based on the values of an existing list
19+
or another iterable (tuple, string, array, range, etc). It does the same task and simplifies the program. Typically, list comprehensions are written in a single line of code.
20+
21+
```python
22+
my_list = [i for i in range(5)]
23+
print(my_list)
24+
```
25+
Output:
26+
```text
27+
[0, 1, 2, 3, 4]
28+
```
29+
List comprehensions are also more efficient computationally than a for loop.
30+
31+
In the code editor, use list comprehension to build `my_efficient_list` from the elements of `my_inefficient_list`
32+
by adding $10$ to each of them. For example, the first element of `my_inefficient_list` is $1 + 10 = 11$,
33+
so the first element of `my_efficient_list` should be $11 + 10 = 21$ and so on.
34+
35+
36+
<div class="hint">
37+
38+
In the example above, we used `i for i in range(5)`. You can modify `i` as you like
39+
right inside this expression. For example, to subtract `5` from every `i`, you can do
40+
`i - 5 for i in range(5)`.
41+
</div>

‎Loops/List Comprehension/task.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
starting_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2+
my_inefficient_list = []
3+
4+
for i in starting_numbers:
5+
my_inefficient_list.append(i + 10)
6+
7+
print(my_inefficient_list)
8+
9+
10+
my_efficient_list = [i + 10 for i in my_inefficient_list]
11+
print(my_efficient_list)

‎Loops/List Comprehension/tests/__init__.py

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import unittest
2+
3+
from task import *
4+
5+
6+
# todo: replace this with an actual test
7+
class TestCase(unittest.TestCase):
8+
def test_add(self):
9+
test_list = [i + 10 for i in my_inefficient_list]
10+
self.assertEqual(test_list, my_efficient_list,
11+
msg="Each element of `my_efficient_list` should equal a corresponding element of `my_inefficient_list` plus 10.")

‎Loops/lesson-info.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
content:
22
- For loop
33
- Loop over a string
4+
- List Comprehension
45
- While loop
56
- Break keyword
67
- Else with loops
78
- Continue keyword
9+

0 commit comments

Comments
 (0)
Please sign in to comment.