Skip to content

script_14.py - List Flattening

Code

See script_14.py for full code.

Explanation

Line 8: from itertools import chain - chain for efficient flattening

Line 11: nested = [[1, 2, 3], [4, 5], [6, 7, 8, 9]] - List containing lists (2 levels of nesting) - Each element is itself a list of integers

Line 14: flat1 = [item for sublist in nested for item in sublist] - Nested list comprehension - Outer loop: for sublist in nested - Inner loop: for item in sublist - Order: outer first, then inner - Reads left-to-right unlike nested for loops

Line 18: flat2 = list(chain(*nested)) - *nested unpacks list into arguments - Equivalent to chain([1,2,3], [4,5], [6,7,8,9]) - chain() concatenates all sublists - Returns iterator, converted to list

Line 22: flat3 = sum(nested, []) - sum() with start value [] (empty list) - Works because lists support + operator - Repeatedly adds: [] + [1,2,3] + [4,5] + ... - Clever but inefficient (creates many intermediate lists)

Line 27: deep_nested = [1, [2, 3, [4, 5]], 6, [7, [8, [9, 10]]]] - Multiple levels of nesting (irregular structure) - Mix of integers and lists

Line 30: def flatten_recursive(lst): - Recursive function definition

Line 31: result = [] - Accumulator for flattened elements

Line 32: for item in lst: - Loop through all elements

Line 33: if isinstance(item, list): - isinstance() checks type at runtime - Returns True if item is a list - Type checking to detect nested lists

Line 34: result.extend(flatten_recursive(item)) - Recursive call for nested lists - .extend() adds all elements from returned list - Different from .append() which would add the list itself

Line 36: result.append(item) - Non-list items added directly - Base case of recursion

Line 47: all_tags = [tag for doc in documents for tag in doc["tags"]] - Nested comprehension flattening nested structure - First loop through documents - Then loop through tags in each document

Line 54: all_tags_chain = list(chain.from_iterable(doc["tags"] for doc in documents)) - .from_iterable() is chain class method - Takes single iterable of iterables - More efficient than unpacking with * - Generator expression: doc["tags"] for doc in documents