This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
python3:list_comprehensions [2019/05/07 14:52] jguerin Added note about tests/storage and generators. |
python3:list_comprehensions [2019/05/07 16:19] (current) jguerin Moved the multiple loop example, switched x*2 to abs(x) in list example. |
||
---|---|---|---|
Line 20: | Line 20: | ||
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81] | [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] | ||
</ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== Example Functionality ===== | ||
==== Multiple Loops ==== | ==== Multiple Loops ==== | ||
- | Note that this basic syntax is easily extended to multiple loops and variables: | ||
<code python> | <code python> | ||
>>> | >>> | ||
Line 28: | Line 31: | ||
</ | </ | ||
- | ===== Examples | + | ====Conditionals==== |
- | Conditionals: | + | |
<code python> | <code python> | ||
>>> | >>> | ||
Line 35: | Line 37: | ||
</ | </ | ||
- | Repetitions: | + | ====Repetitions==== |
<code python> | <code python> | ||
>>> | >>> | ||
Line 41: | Line 43: | ||
</ | </ | ||
- | Processing non-range() iterables: | + | ====Processing non-range() iterables==== |
<code python> | <code python> | ||
>>> | >>> | ||
- | >>> | + | >>> |
- | [-8, -4, 0, 4, 8] | + | [4, 2, 0, 2, 4] |
+ | </ | ||
+ | |||
+ | |||
+ | ====Creating a double list==== | ||
+ | <code python> | ||
+ | >>> | ||
+ | >>> | ||
+ | [[0, 1, 2], [3, 4, 5], [6, 7, 8]] | ||
</ | </ | ||
- | Flattening a double list: | + | ====Flattening a double list==== |
<code python> | <code python> | ||
>>> | >>> |