Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

List comprehension - simple expression

import sys

numbers  = [0, 1, 2, 3]

sqrs = map(lambda n: n*n, numbers)
print(sqrs)         # <map object at 0x7fdcab2f5940>
print(list(sqrs))   # [0, 1, 4, 9]
print(sys.getsizeof(sqrs))
print()

squares = [n*n for n in numbers]
print(squares)   # [0, 1, 4, 9]
print(sys.getsizeof(squares))

Output:

<map object at 0x7fa9cf2eb9e8>
[0, 1, 4, 9]
56

[0, 1, 4, 9]
96