Deep Flatten List

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.

Deep Flatten List

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.

from collections.abc import Iterable

def deep_flatten(lst):
  return ([a for i in lst for a in
          deep_flatten(i)] if isinstance(lst, Iterable) else [lst])
deep_flatten([1, [2], [[3], 4], 5]) ## [1, 2, 3, 4, 5]

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