Thanks to visit codestin.com
Credit goes to github.com

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

+13-1
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)