How to make a flat list out of list of lists (join a list of lists) in Python

2 Answers

0 votes
a_list = [[1, 2, 3], [4, 5], [6], [7], [8, 9]]

lst = []

for ll in a_list:
    for item in ll:
        lst.append(item)

print(lst)


'''
run:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

'''

 



answered Oct 30, 2017 by avibootz
0 votes
a_list = [[1, 2, 3], [4, 5], [6], [7], [8, 9]]

lst = [item for ll in a_list for item in ll]

print(lst)


'''
run:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

'''

 



answered Oct 30, 2017 by avibootz

Related questions

1 answer 165 views
1 answer 261 views
5 answers 299 views
1 answer 276 views
2 answers 206 views
2 answers 186 views
186 views asked Nov 26, 2023 by avibootz
...