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

size of map

One advantage of keeping the result of map as it is is the memory footprint of the map object vs. the generated list.

The map function returns a map object that takes up a fixed amount of memory, regardless of the size of the original list.

If we flatten it using the list function, the size of the generated list will be similar to the size of the original list.

import sys

def double(num):
    return 2 * num

numbers = [7, 2, 4, 1]

double_iterable = map(double, numbers)
double_numbers = list(map(double, numbers))

print(sys.getsizeof(double_iterable))
print(sys.getsizeof(double_numbers))

Output:

48
120