Answers for "python expand nested list"

0

python expand nested list

# credit to the Stack Overflow user in the source link
"""Note: using 'chain' is about 20% faster than nested looping"""
from itertools import chain

nested_list = [[1,2,3], [4,5,6], [7], [8,9]]
expanded_list = list(chain(*nested_list))
expanded_list
>>> [1,2,3,4,5,6,7,8,9]
Posted by: Guest on April-15-2022

Python Answers by Framework

Browse Popular Code Answers by Language