Deep Flatten List | Challenge

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In Python, a list can contain other lists as elements, and those lists can contain even more lists as elements, forming a nested structure. The process of flattening a nested list means to convert it into a one-dimensional list, where all the elements are at the same level. In this challenge, you will be asked to write a function that deep flattens a list, meaning that it will flatten all the nested lists recursively.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/ControlFlowGroup -.-> python/list_comprehensions("`List Comprehensions`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13085{{"`Deep Flatten List | Challenge`"}} python/conditional_statements -.-> lab-13085{{"`Deep Flatten List | Challenge`"}} python/for_loops -.-> lab-13085{{"`Deep Flatten List | Challenge`"}} python/list_comprehensions -.-> lab-13085{{"`Deep Flatten List | Challenge`"}} python/lists -.-> lab-13085{{"`Deep Flatten List | Challenge`"}} python/tuples -.-> lab-13085{{"`Deep Flatten List | Challenge`"}} python/function_definition -.-> lab-13085{{"`Deep Flatten List | Challenge`"}} python/importing_modules -.-> lab-13085{{"`Deep Flatten List | Challenge`"}} python/using_packages -.-> lab-13085{{"`Deep Flatten List | Challenge`"}} python/standard_libraries -.-> lab-13085{{"`Deep Flatten List | Challenge`"}} python/build_in_functions -.-> lab-13085{{"`Deep Flatten List | Challenge`"}} end

Deep Flatten List

Problem

Write a function deep_flatten(lst) that takes a list lst as an argument and returns a new list that is the deep flattened version of lst. The function should use recursion and the isinstance() function with collections.abc.Iterable to check if an element is iterable. If an element is iterable, the function should apply deep_flatten() recursively to that element. Otherwise, the function should return a list containing only that element.

Example

deep_flatten([1, [2], [[3], 4], 5]) ## [1, 2, 3, 4, 5]
deep_flatten([1, [2, [3, [4]]]]) ## [1, 2, 3, 4]
deep_flatten([1, 2, 3, 4]) ## [1, 2, 3, 4]
deep_flatten([]) ## []

Summary

In this challenge, you have learned how to deep flatten a list using recursion and the isinstance() function with collections.abc.Iterable. This technique can be useful when dealing with nested data structures, such as lists of lists.

Other Python Tutorials you may like