3

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)
Komi Sla
  • 41
  • 4
  • 3
    Possible duplicate of ["".join(list) if list contains a nested list in python?](https://stackoverflow.com/questions/9607240/joinlist-if-list-contains-a-nested-list-in-python) – Sayse Jun 21 '18 at 13:05
  • If you wish to store it in a text file and then easily pull it back out, you might consider https://docs.python.org/3.5/library/pickle.html – castis Jun 21 '18 at 13:11

4 Answers4

7

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'
>>> 
Sunitha
  • 11,777
  • 2
  • 20
  • 23
2

One way:

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

print(' '.join([x+y for x, y in boundarylist]))
# eːn ak as
Austin
  • 25,759
  • 4
  • 25
  • 48
-1

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)
Zack Atama
  • 81
  • 7
-1

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'

How this works

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.

Anand Tripathi
  • 14,556
  • 1
  • 47
  • 52
  • 1
    Please no. Repeated string concatenations with `reduce` make the code both hard to read and inefficient. – Aran-Fey Jun 21 '18 at 13:25
  • 1
    Yeah it is hard to read but that depends upon the developer to the developer but coming to inefficient it concatenating inline and then reducing string to common string in other examples list is first iterating to join the tuples then again it is iterating to concat the element with the whitespace character. I think the other thing is inefficient but @Aran-Fey if you have downvoted because of hard to read than its okay. Thanks – Anand Tripathi Jun 21 '18 at 13:30
  • your code without the print statements takes 1.5 µs ± 16.8 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each). The code of the accepted anwser takes 842 ns ± 8.83 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each). In addition your code only works for python 2. In python3 reduce was moved to functools. – error Jun 21 '18 at 15:02