Skip to content

Commit

Permalink
Add example with vs without list comprehension
Browse files Browse the repository at this point in the history
  • Loading branch information
varian97 authored and Akuli committed Jul 25, 2020
1 parent 52073d8 commit 93a00c2
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion basics/lists-and-tuples.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ We'll talk more about loops [in the next chapter](loops.md).
```

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

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

without comprehension:

```python
>>> numbers = [1,2,3,4,5]
>>> numbers_squared = []
>>> for number in numbers:
... numbers_squared.append(number**2)
>>> numbers_squared
[1, 4, 9, 16, 25]
>>>
```

We can also use slicing and indexing to change the content:

```python
Expand Down

0 comments on commit 93a00c2

Please sign in to comment.