Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python nested list comprehension string concatenation

I have a list of lists in python looking like this:

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

I want to come up with a string like this:

a,b;c,d

So the lists should be separated with a ; and the values of the same list should be separated with a ,

So far I tried ','.join([y for x in test for y in x]) which returns a,b,c,d. Not quite there, yet, as you can see.

like image 427
beta Avatar asked Dec 02 '22 16:12

beta


2 Answers

";".join([','.join(x) for x in a])
like image 142
NeoWang Avatar answered Dec 05 '22 06:12

NeoWang


>>> ';'.join(','.join(x) for x in [['a', 'b'], ['c', 'd']])
'a,b;c,d'
like image 21
Dan D. Avatar answered Dec 05 '22 07:12

Dan D.