Flatten Nested Lists with Python

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. Sometimes, we need to flatten a list, which means to convert a nested list into a single list that contains all the elements of the original list. One way to do this is by using the spread operator. In this challenge, you will be asked to implement a function that flattens a list using the spread operator.


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/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13722{{"`Flatten Nested Lists with Python`"}} python/variables_data_types -.-> lab-13722{{"`Flatten Nested Lists with Python`"}} python/conditional_statements -.-> lab-13722{{"`Flatten Nested Lists with Python`"}} python/for_loops -.-> lab-13722{{"`Flatten Nested Lists with Python`"}} python/lists -.-> lab-13722{{"`Flatten Nested Lists with Python`"}} python/tuples -.-> lab-13722{{"`Flatten Nested Lists with Python`"}} python/function_definition -.-> lab-13722{{"`Flatten Nested Lists with Python`"}} python/data_collections -.-> lab-13722{{"`Flatten Nested Lists with Python`"}} python/build_in_functions -.-> lab-13722{{"`Flatten Nested Lists with Python`"}} end

Spread List

Write a function called spread(arg) that takes a list as an argument and returns a new list that contains all the elements of the original list, flattened. If an element of the original list is a list itself, its elements should be added to the new list individually. The function should not modify the original list.

To implement the function, you should loop over the elements of the original list and use the spread operator to add the elements to the new list. If an element is a list, you should use the extend() method to add its elements to the new list. If an element is not a list, you should use the append() method to add it to the new list.

def spread(arg):
  ret = []
  for i in arg:
    ret.extend(i) if isinstance(i, list) else ret.append(i)
  return ret
spread([1, 2, 3, [4, 5, 6], [7], 8, 9]) ## [1, 2, 3, 4, 5, 6, 7, 8, 9]

Summary

In this challenge, you have learned how to use the spread operator to flatten a list in Python. You have also implemented a function that takes a list as an argument and returns a new list that contains all the elements of the original list, flattened. This is a useful technique to know when working with nested lists in Python.

Other Python Tutorials you may like