# 9.6. Comprehension Map¶

## 9.6.1. Example¶

>>> [float(x) for x in range(0,5)]
[0.0, 1.0, 2.0, 3.0, 4.0]


## 9.6.2. Apply Function¶

>>> [pow(2,x) for x in range(0,5)]
[1, 2, 4, 8, 16]

>>> [pow(2,x) for x in range(0,5)]
[1, 2, 4, 8, 16]

>>> [pow(x,x) for x in range(0,5)]
[1, 1, 4, 27, 256]


## 9.6.3. Convert Data¶

>>> 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')]
>>>
>>>
>>> [tuple(row[0:4]) for row in DATA[1:]]
[(5.8, 2.7, 5.1, 1.9),
(5.1, 3.5, 1.4, 0.2),
(5.7, 2.8, 4.1, 1.3),
(6.3, 2.9, 5.6, 1.8),
(6.4, 3.2, 4.5, 1.5),
(4.7, 3.2, 1.3, 0.2)]


## 9.6.4. Map to String¶

>>> DATA = [1, 2, 3]
>>> str(DATA)
'[1, 2, 3]'

>>> [str(x) for x in DATA]
['1', '2', '3']

>>> data = [str(x) for x in DATA]
>>> ','.join(data)
'1,2,3'

>>> ','.join(str(x) for x in DATA)
'1,2,3'


## 9.6.5. Generate CSV¶

>>> DATA = [1, 2, 3]
>>>
>>>
>>> ','.join(DATA)
Traceback (most recent call last):
TypeError: sequence item 0: expected str instance, int found

>>> DATA = [1, 2, 3]
>>>
>>>
>>> ','.join(str(x) for x in DATA)
'1,2,3'


## 9.6.6. Generate CSV Data¶

>>> 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'),
... ]
>>>
>>>
>>> result = [','.join(str(x) for x in row) for row in DATA]
>>> result = '\n'.join(result)
>>>
>>> print(result)
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


## 9.6.7. Parse CSV¶

>>> DATA = '5.8,2.7,5.1,1.9\n5.1,3.5,1.4,0.2\n5.7,2.8,4.1,1.3'
>>>
>>> result = []
>>>
>>> for row in DATA.splitlines():
...     row = row.split(',')
...     result.append(row)
>>>
>>> print(result)
[['5.8', '2.7', '5.1', '1.9'],
['5.1', '3.5', '1.4', '0.2'],
['5.7', '2.8', '4.1', '1.3']]

>>> DATA = '5.8,2.7,5.1,1.9\n5.1,3.5,1.4,0.2\n5.7,2.8,4.1,1.3'
>>>
>>> [row.split(',') for row in DATA.splitlines()]
[['5.8', '2.7', '5.1', '1.9'],
['5.1', '3.5', '1.4', '0.2'],
['5.7', '2.8', '4.1', '1.3']]

>>> DATA = '5.8,2.7,5.1,1.9\n5.1,3.5,1.4,0.2\n5.7,2.8,4.1,1.3'
>>>
>>> [[float(x) for x in row.split(',')] for row in DATA.splitlines()]
[[5.8, 2.7, 5.1, 1.9],
[5.1, 3.5, 1.4, 0.2],
[5.7, 2.8, 4.1, 1.3]]

>>> DATA = '5.8,2.7,5.1,1.9,virginica\n5.1,3.5,1.4,0.2,setosa\n5.7,2.8,4.1,1.3,versicolor'
>>>
>>> def convert(x):
...     try:
...         return float(x)
...     except ValueError:
...         return x
>>>
>>> [[convert(x) for x in row.split(',')] for row in DATA.splitlines()]
[[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']]


## 9.6.8. Use Case - 0x01¶

• Raise number to the n-th power

>>> [pow(x,2) for x in range(0,5)]
[0, 1, 4, 9, 16]

>>> [x**2 for x in range(0,5)]
[0, 1, 4, 9, 16]

>>> [pow(2,x) for x in range(0,5)]
[1, 2, 4, 8, 16]

>>> [2**x for x in range(0,5)]
[1, 2, 4, 8, 16]


## 9.6.9. Use Case - 0x02¶

• Map 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'},
... ]
>>>
>>>
...           'lastname': user['name'].split()[1]}
...           for user in users
>>>
[{'firstname': 'Melissa', 'lastname': 'Lewis'},
{'firstname': 'Mark', 'lastname': 'Watney'},
{'firstname': 'Alex', 'lastname': 'Vogel'}]


## 9.6.10. Use Case - 0x03¶

>>> 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'},
... ]
>>>
>>>
...            'lastname': user['name'].split()[1][0]+'.'}
...            for user in users
>>>
[{'firstname': 'Melissa', 'lastname': 'L.'},
{'firstname': 'Mark', 'lastname': 'W.'},
{'firstname': 'Alex', 'lastname': 'V.'}]


## 9.6.11. Use Case - 0x04¶

• Assignment expression

>>> 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 = [{'firstname': fname, 'lastname': lname}
...            for user in users
...            and (name := user['name'].split())
...            and (fname := name[0].capitalize())
...            and (lname := f'{name[1][0]}.')]
>>>
[{'firstname': 'Melissa', 'lastname': 'L.'},
{'firstname': 'Mark', 'lastname': 'W.'},
{'firstname': 'Alex', 'lastname': 'V.'}]


## 9.6.12. Use Case - 0x05¶

• Assignment expression

>>> 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'},
... ]
>>>
>>>