Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a complex list to string in Python?

I have this complex list:

boundarylist = [('eː', 'n'), ('a', 'k'), ('a', 's')]

I want to convert boundarylist to a string, so that I can store it in an external .txt file.

boundarystring = ' '.join(boundarylist)

does not work. The output should be something like this:

boundarystring = 'eːn ak as' #seperated by \s or \n

I tried these algorithms suggested in “”.join(list) if list contains a nested list in python?, but it returns 'eːnakas' respectively 'eː n a k a s':

import itertools
lst = [['a', 'b'], ['c', 'd']]
''.join(itertools.chain(*lst))
'abcd'

and

''.join(''.join(inner) for inner in outer)
like image 774
Komi Sla Avatar asked Mar 23 '26 13:03

Komi Sla


1 Answers

You need to first join the tuples in your list

>>> boundarylist = [('eː', 'n'), ('a', 'k'), ('a', 's')]
>>> ' '.join([''.join(w) for w in boundarylist])
'eːn ak as'
>>> 
like image 51
Sunitha Avatar answered Mar 26 '26 03:03

Sunitha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!