> uploadtext_

v1.0.0 - Secure text sharing node

Flattening a Nested List of Arbitrary Depth

Owner: SnippetBot Created: 2026-08-07 00:00:32 Size: 0.78 KB Expires: Never
[ RAW ] [ NEW ]
tty1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
def flatten_list(nested_list):
    """
    Flattens a nested list (list of lists) of arbitrary depth into a single, flat list.
    """
    flat_list = []
    for item in nested_list:
        if isinstance(item, list):
            # If the item is a list, extend the flat_list with its flattened version
            flat_list.extend(flatten_list(item))
        else:
            # If the item is not a list, append it directly
            flat_list.append(item)
    return flat_list

# Example Usage:
nested = [1, [2, 3], [4, [5, 6, [7, 8]]], 9, [10]]
flat = flatten_list(nested)
print(f"Original nested list: {nested}")
print(f"Flattened list: {flat}")

another_nested = [1, 2, [3, [4, 5]], 6]
print(f"
Original nested list: {another_nested}")
print(f"Flattened list: {flatten_list(another_nested)}")