12.7. Comprehension Filter

12.7.1. Recap

>>> result = []
>>>
>>> for x in range(0,5):
...     if x % 2 == 0:
...         result.append(x)
>>>
>>> print(result)
[0, 2, 4]

12.7.2. Syntax

>>> 
... result = [<RETURN> for <VARIABLE> in <ITERABLE> if <CONDITION>]

12.7.3. Example

>>> result = [x for x in range(0,5) if x%2==0]
>>>
>>> print(result)
[0, 2, 4]

12.7.4. Filter list[tuple]

>>> DATA = [
...     ('sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'),
...     (5.8, 2.7, 5.1, 1.9, 'virginica'),
...     (5.1, 3.5, 1.4, 0.2, 'setosa'),
...     (5.7, 2.8, 4.1, 1.3, 'versicolor'),
...     (6.3, 2.9, 5.6, 1.8, 'virginica'),
...     (6.4, 3.2, 4.5, 1.5, 'versicolor'),
...     (4.7, 3.2, 1.3, 0.2, 'setosa'),
...     (7.0, 3.2, 4.7, 1.4, 'versicolor'),
... ]
>>>
>>>
>>> [row for row in DATA[1:] if row[-1] == 'setosa']
[(5.1, 3.5, 1.4, 0.2, 'setosa'), (4.7, 3.2, 1.3, 0.2, 'setosa')]
>>>
>>> [row[0:4] for row in DATA[1:] if row[-1] == 'setosa']
[(5.1, 3.5, 1.4, 0.2), (4.7, 3.2, 1.3, 0.2)]

12.7.5. Filter list[dict]

>>> users = [
...     {'is_admin': True,  'name': 'Melissa Lewis'},
...     {'is_admin': True,  'name': 'Mark Watney'},
...     {'is_admin': False, 'name': 'Rick Martinez'},
...     {'is_admin': True,  'name': 'Alex Vogel'},
... ]
>>>
>>>
>>> admins = []
>>>
>>> for user in users:
...     if user['is_admin']:
...         admins.append(user['name'])
>>>
>>> print(admins)
['Melissa Lewis', 'Mark Watney', 'Alex Vogel']
>>> users = [
...     {'is_admin': True,  'name': 'Melissa Lewis'},
...     {'is_admin': True,  'name': 'Mark Watney'},
...     {'is_admin': False, 'name': 'Rick Martinez'},
...     {'is_admin': True,  'name': 'Alex Vogel'},
... ]
>>>
>>>
>>> admins = [user['name']
...           for user in users
...           if user['is_admin']]
>>>
>>> print(admins)
['Melissa Lewis', 'Mark Watney', 'Alex Vogel']

12.7.6. Good Practices

>>> result = [pow(x, 2) for x in range(0, 5) if x % 2 == 0]
>>> result = [pow(x,2) for x in range(0,5) if x%2==0]
>>> result = [pow(x,2)
...           for x in range(0,5)
...               if x % 2 == 0]
>>> result = [pow(x,2)
...           for x in range(0,5)
...           if x % 2 == 0]

12.7.7. Assignments

"""
* Assignment: Comprehension Filter Even
* Type: class assignment
* Complexity: easy
* Lines of code: 2 lines
* Time: 3 min

English:
    1. Use list comprehension
    2. Generate `result: list[int]`
       of even numbers from 5 to 20 (without 20)
    3. Run doctests - all must succeed

Polish:
    1. Użyj rozwinięcia listowego
    2. Wygeneruj `result: list[int]`
       parzystych liczb z przedziału 5 do 20 (bez 20)
    3. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * `range()`
    * `%`
    * `==`

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert type(result) is list, \
    'Result should be a list'

    >>> assert all(type(x) is int for x in result), \
    'Result should be a list of int'

    >>> result
    [6, 8, 10, 12, 14, 16, 18]
"""

# Even numbers from 5 to 20 (without 20)
# type: list[int]
result = ...