Flatten a list of lists to a single list

PHOTO EMBED

Wed Oct 20 2021 02:49:37 GMT+0000 (UTC)

Saved by @ianh

# flatten list of lists
ls = [['a','b','c'],['d','e','f'],['g','h','i']]
 
# iterate through list of lists in a nested loop
flat_ls = []
for i in ls:
    for j in i:
        flat_ls.append(j)
 
# print
print("Original list:", ls)
print("Flattened list:", flat_ls)
content_copyCOPY

https://datascienceparichay.com/article/python-flatten-a-list-of-lists-to-a-single-list/