Learn practical skills, build real-world projects, and advance your career

A gentle guide to Python features that I didn't know exist or was too afraid to use.

1. Lambda, map, filter, reduce

The lambda keyword is used to create inline functions. square_fn and square_ld below are identical:

def square_fn(x):
    return x * x

square_ld = lambda x : x * x

for i in range(10):
    assert square_fn(i) == square_ld(i)

Its quick declaration makes lambda function ideal for using in callbacks, functions that are passed as arguments to other functions. They are especially useful when used in junction with functions like map, filter, and reduce.

map(fn, iterable) applies the fn to all elements of the iterable (e.g. list, set, dictionary, tuple, string) and returns a map object.