From a035aed9adc448bcc85b3af42d80d2821a7c1d7d Mon Sep 17 00:00:00 2001 From: Agilarasu Date: Sun, 29 Dec 2024 16:26:49 +0530 Subject: [PATCH] Add List Manipulation function to flatten unevenly nested lists in Python --- public/data/python.json | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/public/data/python.json b/public/data/python.json index b617dbda..0f648804 100644 --- a/public/data/python.json +++ b/public/data/python.json @@ -48,6 +48,28 @@ "tags": ["python", "list", "flatten", "utility"], "author": "technoph1le" }, + { + "title": "Flatten Unevenly Nested Lists", + "description": "Converts unevenly nested lists of any depth into a single flat list.", + "code": [ + "def flatten(nested_list):", + " \"\"\"", + " Flattens unevenly nested lists of any depth into a single flat list.", + " \"\"\"", + " for item in nested_list:", + " if isinstance(item, list):", + " yield from flatten(item)", + " else:", + " yield item", + "", + "# Usage:", + "nested_list = [1, [2, [3, 4]], 5]", + "flattened = list(flatten(nested_list))", + "print(flattened) # Output: [1, 2, 3, 4, 5]" + ], + "tags": ["python", "list", "flattening", "nested-lists", "depth", "utilities"], + "author": "agilarasu" + }, { "title": "Remove Duplicates", "description": "Removes duplicate elements from a list while maintaining order.",