python nested list comprehension string concatenation

4.3k views Asked by At

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.

3

There are 3 answers

2
NeoWang On BEST ANSWER
";".join([','.join(x) for x in a])
0
Padraic Cunningham On

To do it functionally you could use map:

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


print(";".join(map(".".join, l)))
a.b;c.d
0
Dan D. On
>>> ';'.join(','.join(x) for x in [['a', 'b'], ['c', 'd']])
'a,b;c,d'