958 B
958 B
id, source_exercise_id, title, section, source_path, source_repo, source_commit, student_visible_solution, has_private_solution, skip
| id | source_exercise_id | title | section | source_path | source_repo | source_commit | student_visible_solution | has_private_solution | skip |
|---|---|---|---|---|---|---|---|---|---|
| practical-python-2.19 | 2.19 | List comprehensions | 2.6 List Comprehensions | 02_Working_with_data/06_List_comprehension.md | https://github.com/dabeaz-course/practical-python | 93dca856b41c61a0a0f85ae334116e4c125629ea | false | false | false |
Exercise 2.19: List comprehensions
Source: Practical Python Programming,
02_Working_with_data/06_List_comprehension.md.
Exercise 2.19: List comprehensions
Try a few simple list comprehensions just to become familiar with the syntax.
>>> nums = [1,2,3,4]
>>> squares = [ x * x for x in nums ]
>>> squares
[1, 4, 9, 16]
>>> twice = [ 2 * x for x in nums if x > 2 ]
>>> twice
[6, 8]
>>>
Notice how the list comprehensions are creating a new list with the data suitably transformed or filtered.