Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comprehension #3022

Merged
merged 6 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Types of change:
### Changed
- [Html - Link Relative Paths - Change part of PQ as it wasn't worder properly](https://github.com/enkidevs/curriculum/pull/2985)
- [Python - Format Text Paragraphs With Textwrap - Make the fill method more clear](https://github.com/enkidevs/curriculum/pull/2981)
- [Python - Comprehension - Move single-line commands to a single line, update indentation in codeblocks from 4 to 2 spaces](https://github.com/enkidevs/curriculum/pull/3022)
- [Python - Iterators - Move single-line commands to a single line, update indentation in codeblocks from 4 to 2 spaces](https://github.com/enkidevs/curriculum/pull/3027)

## January 4th 2022
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ cube_dict = {x: x ** 3 for x in num_list}

```

Now if we print cube_dict, we get:
Now, if we print `cube_dict`, we get:

```python
for k, v in cube_dict.items():
print(k, v)
print(k, v)
# output
# 1 1
# 2 8
Expand All @@ -58,17 +58,18 @@ print(lcase_freqs)
# partial output ...

{'u': 0, 'q': 0, 'w': 0, 'o': 0, \
'b': 0, 'c': 0, 't': 0, 'h': 0, \
'b': 0, 'c': 0, 't': 0, 'h': 0, \
...
'g': 0, 'a': 0, 'n': 0}
'g': 0, 'a': 0, 'n': 0}

# Check it is correct:
lfk = list(lcase_freqs.keys())
lfk.sort()
print(lfk)
['a', 'b', 'c', 'd', 'e', 'f', \
'g', 'h', 'i', 'j', 'k', 'l', \
'm', 'n', 'o', 'p','q', 'r', \
's', 't', 'u', 'v', 'w', 'x', \
'y', 'z']
'g', 'h', 'i', 'j', 'k', 'l', \
'm', 'n', 'o', 'p','q', 'r', \
's', 't', 'u', 'v', 'w', 'x', \
'y', 'z']

```
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ Use list comprehension to add one and divide by two [(x + 1) / 2] for all elemen

```python
l = [1,2,3,4,5]
x = [((x+1)/2) ??? x % 2 \
??? x ??? x in ???]
x = [((x+1)/2) ??? x % 2 ??? x ??? x in ???]
```

- if
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,23 @@ Since a list comprehension can take any **expression** as its initial expression
These are often useful, but are often used to work with matrices.

```python
matrix = [[1, 2, 3], [4, 5, 6], \
[7, 8, 9]]

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
```

Say we want to create another matrix with values equal to the squares of each element in the original matrix:

```python
matrix2 = [[x**2 for x in row] for \
row in matrix]
#matrix2 = [[1, 4, 9], [16, 25, 36],\
# [49, 64, 81]]
matrix2 = [[x**2 for x in row] for row in matrix]
#matrix2 = [[1, 4, 9], [16, 25, 36], [49, 64, 81]]
```

A more advanced list comprehension with two for clauses and two if clauses:

```python
lc = [ (x, y) for x in \
range(10) if x % 2 == 0 \
for y in range(20) if \
y % 3 == 0 ]
range(10) if x % 2 == 0 \
for y in range(20) if \
y % 3 == 0 ]
# lc
# [(0, 0), (0, 3), (0, 6), \
# (0, 9), (0, 12), (0, 15), (0, 18),\
Expand All @@ -70,7 +66,6 @@ Use nested list comprehension to generate a list of tuples, where the first elem
Ex: (1,1),(1,2),(1,3),...(9,7),(9,8),(9,9).

```python

l = [??? for x in range(10)\
if ??? for y in ???]
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,19 @@ Imagine we have the following list:

```python
my_list = [1, 2, 3, 4, 5, 6, 7, 8]

```

And we need a set containing only even numbers in the list. This can be easily achieved with **set comprehension**:

```python
even_set = {x for x in my_list if x%2 == 0}

```

We can now check the result:

```python
print(even_set)
# {8, 2, 4, 6}

```

Note that the above operation would work even if my_list contained some duplicate values, e.g:
Expand All @@ -67,10 +64,8 @@ since sets by definition do not allow duplicates.
Fill in the following code snippet. It creates a new set that contains elements of list `l` that are even and adds one and divides by two the odd numbers:

```python

l = [10, 11, 13, 14, 18, 19]
new_set = {x ??? x % 2 == 0 else/
??? for x ??? l}
new_set = {x ??? x % 2 == 0 else ??? for x ??? l}
```

- if
Expand All @@ -88,7 +83,6 @@ new_set = {x ??? x % 2 == 0 else/
What will the `odd_set` look like after we run the following code snippet?

```python

l = [1,3,3,2,4,5,5,8,9]
odd_set = {x for x in l if x % 2}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ If you need to lowercase all the input strings:
```python
lower_list = []
for word in input_list:
lower_list.append(word.lower())
lower_list.append(word.lower())
```

Instead, you can use `map()` to push the loop into compiled C code:
Expand All @@ -47,8 +47,7 @@ lower_list = map(str.lower, input_list)
Also, in Python 2.0 or above, there are list comprehensions. List comprehension are the "pythonic" way to approach this situation. `map()` is more often used in JavaScript. We recommend usage of list comprehension:

```python
lower_list = [word.lower() \
for word in input_list]
lower_list = [word.lower() for word in input_list]
```

They are both more efficient than simple `for` loop statement.
Expand All @@ -63,8 +62,7 @@ Use list comprehension to modify a list of characters such that all its elements
```python
strings = ['a', 'e', 'i', 'o', 'u']

lower_list = [word.??? \
for word in ???]
lower_list = [word.??? for word in ???]
```

- upper()
Expand Down