Skip to content

Commit

Permalink
feat: add list comprehension tutorial (#131)
Browse files Browse the repository at this point in the history
* add list comprehension tutorial

* Update README.md

Co-authored-by: Sarah Abderemane <[email protected]>

* Update lists/comprehension.py

Co-authored-by: Sarah Abderemane <[email protected]>

* Update lists/comprehension.py

Co-authored-by: Sarah Abderemane <[email protected]>

* Update lists/comprehension.py

Co-authored-by: Sarah Abderemane <[email protected]>

* Update lists/comprehension.py

Co-authored-by: Sarah Abderemane <[email protected]>

* fix challenge steps

* rename file for consistency

* fix trailing space

Co-authored-by: Sarah Abderemane <[email protected]>
  • Loading branch information
jrafaaael and sabderemane authored Oct 6, 2022
1 parent 87ae8ec commit 6fb8c43
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Each file contains:
|How to alter a list | [alterning-lists.py](lists/altering-lists.py) |
|How to declare a list | [declaring-lists.py](lists/declaring-lists.py) |
|How to index a list | [indexing-lists.py](lists/indexing-lists.py) |
|List comprehension | [list-comprehension.py](lists/list-comprehension.py) |
| **Conditions** |
|How to make conditions | [if-else.py](conditions/if-else.py) |
| **Loops** |
Expand Down
53 changes: 53 additions & 0 deletions lists/list-comprehension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# ------------------------------------------------------------------------------------
# Tutorial: List comprehension
# ------------------------------------------------------------------------------------
# It's a Python feature to iterate and create a list from some other list in a
# shorter and declarative way than loops.


# In general there is a very common for loop structure that looks like this:
for_loop_squares = []

for i in range(10):
for_loop_squares.append(i * i)

print(for_loop_squares) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]


# With list comprehension you can do the exact same thing in one line.
# the syntax is:
# new_list = [expression for member in iterable]
comprehension_squares = [x * x for x in range(10)]

print(comprehension_squares) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]


# You can even add conditionals to the list comprehension, the condition is place at the end of the list comprehension, just like that:
# new_list = [expression for member in iterable if condition]
comprehension_even_squares_numbers = [x * x for x in range(10) if x % 2 == 0]

print(comprehension_even_squares_numbers) # [0, 4, 16, 36, 64]


# But if you can do if/else logic, you need to place the condition before the for loop just like that:
comprehension_even_squares_numbers_and_odd_numbers_is_zero = [x * x if x % 2 == 0 else 0 for x in range(10)]

print(comprehension_even_squares_numbers_and_odd_numbers_is_zero) # [0, 0, 4, 0, 16, 0, 36, 0, 64, 0]


# We can summarize the 3 formats of list comprehension. The basic format of a list comprehension is like:
# new_list = [expression for member in iterable]

# With only one condition
# new_list = [expression for member in iterable if condition]

# With if/else logic:
# new_list = [expression if condition else other_expression for x in sequence]


# ------------------------------------------------------------------------------------
# Challenge: FIZZBUZZ
# Create a list comprehesion that returns the numbers from 0 to 100 but:
# 1 -. if the number is divisible by 3 and 5, append to the list the word "FizzBuzz"
# 2 -. else, append to the list the number
# ------------------------------------------------------------------------------------

0 comments on commit 6fb8c43

Please sign in to comment.