How to make a flat list out of list of lists?

Technology CommunityCategory: PythonHow to make a flat list out of list of lists?
VietMX Staff asked 3 years ago

Given a list of lists l consider:

flat_list = []
for sublist in l:
    for item in sublist:
        flat_list.append(item)

Or using lambda function:

flatten = lambda l: [item for sublist in l for item in sublist]
print(flatten([[1],[2],[3],[4,5]]))
# Output: [1, 2, 3, 4, 5]