tags:

views:

764

answers:

5

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!

+3  A: 

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])
Magnus Hoff
A: 

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']])
Bastien Léonard
+1  A: 

Easy:

x = [[u'O/ABC'], [u'noez/ABC'], [u'!/ABC']] 
print ' '.join(y[0] for y in x)
Joel
I think this is the better, it is clear and doesn't require you to load another module.Btw, I thik you already have x (the list of lists)
markuz
A: 

I forgot to mention.

The list is derived from a single variable tokentag.

When I split tokentag via test = tokentag.split() I get the lists I mentioned.

You can edit the question to add additional information. This way it is easier to see :)
Magnus Hoff
and then delete this non-answer
SilentGhost
+3  A: 

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))
JSN