Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Merging only the sublists but not all of them into one list in Python

Could somebody please tell me how can I merge the following list:

[[['a', 'b'], ['b', 'a'], ['c', 'c']], [['d', 'e'], ['e', 'd']]]

into:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[['ab', 'ba', 'cc'], ['de', 'ed']]?

Thank you!

>Solution :

IIUC, you need to map the join on all the sublists:

l = [[['a', 'b'], ['b', 'a'], ['c', 'c']], [['d', 'e'], ['e', 'd']]]

out = [list(map(''.join, x)) for x in l]

Or:

out = [[''.join(i) for i in x] for x in l

Output: [['ab', 'ba', 'cc'], ['de', 'ed']]

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading