<jupyter_start><jupyter_text>Elements Of Functional Programming Most of Python’s purely functional built-in functions return a list rather than an iterable in Python 2, making them less memory efficient than their Python 3.x equivalents. If you’re stuck with Python 2, check the `itertools` module. It provides an iterator-based version of many of these functions – `itertools.izip()`, `itertools.imap()`, `itertools.ifilter()`, and so on.  `map()`<jupyter_code>squares = map(lambda num: num * num, [0, 1, 2, 3, 4])
squares
list(squares)
# equivalent using list comprehensions
squares = [num * num for num in [0, 1, 2, 3, 4]]
squares<jupyter_output><empty_output><jupyter_text>`filter()`<jupyter_code>hwords = filter(lambda word: word.startswith('H'), ['Hello', 'World'])
list(hwords)
# equivalent using list comprehensions
hwords = [word for word in ['Hello', 'World'] if word.startswith('H')]
hwords<jupyter_output><empty_output><jupyter_text>`enumerate()`This function is useful when you need to write code that refers to array indexes.<jupyter_code>words = ['I', 'like', 'Python']

for i, word in enumerate(words):
    print(f'Word #{i}: "{word}"')<jupyter_output>Word #0: "I"
Word #1: "like"
Word #2: "Python"<jupyter_text>`sorted()`<jupyter_code>items = [('a', 2), ('c', 1), ('b', 4)]
sorted(items)
sorted(items, key=lambda t: t[1])<jupyter_output><empty_output><jupyter_text>`any()` and `all()`<jupyter_code>nums = [0, 1, 3, -1]

if any(map(lambda n: n < 0, nums)):
    print('At least one item is a negative number')
if all(map(lambda n: n > 0, nums)):
    print('All items are positive numbers')<jupyter_output><empty_output><jupyter_text>`zip()`It's useful when you need to combine a list of keys and a list of values into a dict.<jupyter_code>keys = ['foo', 'bar']
map(len, keys)
zip(keys, map(len, keys))
list(zip(keys, map(len, keys)))
dict(zip(keys, map(len, keys)))<jupyter_output><empty_output><jupyter_text>Finding an item using `first()``first` is a small Python package with a simple function that returns the first true value from an iterable, or None if there is none.<jupyter_code>from first import first

first([0, False, None, [], (), 42])
nums = [-1, 0, 1, 2]
first(nums)
first(nums, key=lambda n: n > 0)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1531651173085436, 'fraction_numerical': 0.015050907481186366, 'mean_word_length': 6.337662337662338, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '64623', 'license': 'MIT', 'n_tokens_mistral': 833, 'n_tokens_neox': 785, 'n_words': 258, 'path': '16_elements_of_functional_programming.ipynb', 'repo_name': 'akrisanov/python_notebook', 'size': '2259'}