Skip to content

Commit 93a00c2

Browse files
varian97Akuli
authored andcommitted
Add example with vs without list comprehension
1 parent 52073d8 commit 93a00c2

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

basics/lists-and-tuples.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ We'll talk more about loops [in the next chapter](loops.md).
142142
```
143143

144144
Another useful things about list is **comprehension**.
145-
**Comprehension** is a way to loop the list in single line. It makes our code more pythonic.
145+
**Comprehension** is a way to construct a list in single line. It makes our code more clean, shorter and easier to read.
146146

147147
```python
148148
>>> numbers = [1,2,3,4,5]
@@ -152,6 +152,18 @@ Another useful things about list is **comprehension**.
152152
>>>
153153
```
154154

155+
without comprehension:
156+
157+
```python
158+
>>> numbers = [1,2,3,4,5]
159+
>>> numbers_squared = []
160+
>>> for number in numbers:
161+
... numbers_squared.append(number**2)
162+
>>> numbers_squared
163+
[1, 4, 9, 16, 25]
164+
>>>
165+
```
166+
155167
We can also use slicing and indexing to change the content:
156168

157169
```python

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy