Reputation:
Howdy, I've got multiple lists. For example:
[u'This/ABC']
[u'is/ABC']
[u'not/ABC']
[u'even/ABC']
[u'close/ABC']
[u'to/ABC']
[u'funny/ABC']
[u'./ABC']
[u'O/ABC']
[u'noez/ABC']
[u'!/ABC']
I need to join this List to
This/ABC is/ABC not/ABC even/ABC close/ABC to/ABC funny/ABC ./ABC
O/ABC noez/ABC !/ABC
How do I do that please? Yes, with the empty space in between!
Upvotes: 3
Views: 6162
Reputation: 1257
To join lists, try the chain function in the module itertools, For example, you can try
import itertools
print ' '.join(itertools.chain(mylist))
if the new line between the two lists are intentional, then add '\n' at the end of the first list
import itertools
a = [[u'This/ABZ'], [u'is/ABZ'], ....]
b = [[u'O/ABZ'], [u'O/noez'], ...]
a.append('\n')
print ' '.join(itertools.chain(a + b))
Upvotes: 3
Reputation: 22099
If you put them all in a list, for example like this:
a = [
[u'This/ABC'],
[u'is/ABC'],
...
]
You can get your result by adding all the lists and using a regular join on the result:
result = ' '.join(sum(a, []))
After re-reading the question a couple of times, I suppose you also want that empty line. This is just more of the same. Add:
b = [
[u'O/ABC'],
[u'HAI/ABC'],
...
]
lines = [a, b]
result = '\n\n'.join([' '.join(sum(line, [])) for line in lines])
Upvotes: 6
Reputation: 885
Easy:
x = [[u'O/ABC'], [u'noez/ABC'], [u'!/ABC']]
print ' '.join(y[0] for y in x)
Upvotes: 1
Reputation: 61833
If you put all your lists in one list, you can do it like this:
' '.join(e[0] for e in [[u'This/ABC'], [u'is/ABC']])
Upvotes: 0