Bits of Python Information

Use generator expressions for large comprehensions

List comprehensions can cause problems for large inputs by using too much memory. Generator expressions avoid memory issues by producing outputs one at a time as an iterator.

squares_up_to_one_million = [num * num for num in range(1000000)]

# instead of above which will store all the one million number in memory, 
# use generator expression below 
squares_up_to_one_million = (num * num for num in range(1000000))

# this returns you an generator iterator instead of all the numbers 
# so that you can use as you need
squares_up_to_one_million
# <generator object <genexpr> at 0x107b491a8>

# For example you need only the first 100 squares from the result
for _ in range(100):
    print(next(squares_up_to_one_million))

List comprehension instead of map and filter

List comprehensions provide a concise way to create lists. List comprehensions are clearer than the map and filter built-in functions because they don't require extra lambda expressions.

# syntax: [expression for item in iterable if conditional]

# instead of using
doubled_numbers = list(map(lambda x: x * 2, range(10)))

# use
doubled_numbers = [x * 2 for x in range(10)]

# both gives the output
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

Virtual environment

Main purpose of Python virtual environments is to create an isolated working environment for your projects with different dependencies. Each virtual environment has its own Python binary and can have its own independent set of installed Python packages. You can find more information here and here.

# create virtual environment with the name say project_venv
python3 -m venv project_venv

# activate the environment
source project_venv/bin/activate

# do whatever you need in this isolated environment ...

# deactivate 
(project_venv) $ deactivate