Reputation: 41
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)
Upvotes: 3
Views: 466
Reputation: 16146
We can use reduce method for it.
boundarylist = [('eː', 'n'), ('a', 'k'), ('a', 's')]
print reduce(lambda x,y:"".join(x) + " " + "".join(y) , boundarylist)
Output
'eːn ak as'
Here we are actually joining the tuple that is inside the list using .join()
method and then using reduce()
function we are taking two arguments and concatenating them with " " space character that will satisfy your use case.
Upvotes: -1
Reputation: 81
here's what I tried, iterated through the list then through the tuple;
boundarylist = [('eː', 'n'), ('a', 'k'), ('a', 's')]
new_list = []
for tuple_ in boundarylist:
for i in tuple_:
new_list.append(i)
boundarylist = ' '.join(new_list)
print(boundarylist)
Upvotes: -1
Reputation: 26039
One way:
boundarylist = [('eː', 'n'), ('a', 'k'), ('a', 's')]
print(' '.join([x+y for x, y in boundarylist]))
# eːn ak as
Upvotes: 2
Reputation: 12015
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'
>>>
Upvotes: 7