function flatten
Recursively flattens an arbitrarily nested sequence containing lists, tuples, and dictionaries into a single-level generator of non-sequence elements.
/tf/active/vicechatdev/patches/util.py
2284 - 2308
simple
Purpose
This function is designed to flatten deeply nested data structures by recursively traversing through lists, tuples, and dictionaries, yielding individual elements that are not themselves sequences. It's useful for processing hierarchical data structures, extracting all leaf values, or preparing nested data for linear processing. Inspired by pandas' internal flatten utility.
Source Code
def flatten(line):
"""
Flatten an arbitrarily nested sequence.
Inspired by: pd.core.common.flatten
Parameters
----------
line : sequence
The sequence to flatten
Notes
-----
This only flattens list, tuple, and dict sequences.
Returns
-------
flattened : generator
"""
for element in line:
if any(isinstance(element, tp) for tp in (list, tuple, dict)):
yield from flatten(element)
else:
yield element
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
line |
- | - | positional_or_keyword |
Parameter Details
line: A sequence (list, tuple, dict, or any iterable) that may contain nested sequences. The function will recursively traverse this structure to extract all non-sequence elements. Can be arbitrarily nested with any combination of lists, tuples, and dictionaries.
Return Value
Returns a generator object that yields individual elements from the flattened sequence. Each yielded element is a non-sequence item (not a list, tuple, or dict) found at any level of nesting in the input. The generator produces elements in depth-first traversal order. For dictionaries, only the keys are yielded during iteration.
Usage Example
# Basic usage with nested lists
nested_list = [1, [2, 3, [4, 5]], 6]
flattened = list(flatten(nested_list))
print(flattened) # Output: [1, 2, 3, 4, 5, 6]
# Mixed nested structures
mixed = [1, (2, 3), [4, [5, (6, 7)]], 8]
result = list(flatten(mixed))
print(result) # Output: [1, 2, 3, 4, 5, 6, 7, 8]
# With dictionaries (yields keys)
with_dict = [1, {'a': 1, 'b': 2}, [3, 4]]
result = list(flatten(with_dict))
print(result) # Output: [1, 'a', 'b', 3, 4]
# Using as generator for memory efficiency
for item in flatten([[1, 2], [3, [4, 5]]]):
print(item) # Prints: 1, 2, 3, 4, 5 (one per line)
Best Practices
- The function returns a generator, so use list() to convert to a list if you need all values at once, or iterate directly for memory efficiency
- When flattening dictionaries, only the keys are yielded, not the values. If you need values, extract them before flattening
- The function only flattens list, tuple, and dict types. Other iterable types (like strings, sets) are treated as leaf elements and not flattened
- Be cautious with deeply nested structures as the recursive nature could lead to stack overflow with extreme nesting depths
- The function uses 'yield from' for efficient recursive generation without building intermediate lists
- Strings are not flattened, so 'abc' will be yielded as a single element, not as individual characters
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function group_select 40.5% similar
-
function lzip 39.7% similar
-
function clean_for_json_v8 39.4% similar
-
function unique_zip 39.0% similar
-
function clean_for_json_v1 37.0% similar