Home
About
Compiler
Random
Beginner
programming
languages
python-primer
Data-Types
math
Intermediate
datetime
Exceptions
RegEx
Functions
dunder-methods
OOP
Files
Home
About
Compiler
Tutorials
Random
Beginner
programming
languages
python-primer
Data-Types
math
Intermediate
datetime
Exceptions
RegEx
Functions
dunder-methods
OOP
Files
Learn Python and related technologies
×
‹
Prev
Next
›
recursively flatten a list
Share
Share Code Link
×
After you save changes, the snippet will be publicly available to anyone with the link that will be generated.
Give the snippet a concise descriptive title:
e.g A function to add two numbers
Copied!
RUN
waiting
main.py
def recursive_flatten(target_list, flat_list = []): for item in target_list: if not isinstance(item, list): flat_list.append(item) else: recursive_flatten(item, flat_list) #recur on sublists return flat_list nested_list = ['one', ['two', ['three', 'four'], 'five'], 'six', [['seven', ['eight', 'nine' ]] ] ] flat_list = recursive_flatten(nested_list) print(flat_list)
Output: