Sandi Milicevic
Sandi Milicevic

Reputation: 23

Adding newlines in a list of lists

Does anybody know the syntax of turning a list of lists into something that looks like what I have drawn below?

The list of lists would look like so:

list = [0, [~, ~, ~], 1, [~, ~, ~], 2, [~, ~, ~] ]

And this is the desired output:

0 ~ ~ ~
1 ~ ~ ~
2 ~ ~ ~   

I've seen other people ask a similar question however their sub-lists only had one element, and there were no integer elements in front of each sub-list either, and so they used the following in order to obtain the result that I need:

"\n".join(item[0] for item in list)

I'm not sure how to manipulate the above line of code to solve my specific problem. I tried changing

item[0] to item[0:len(sub_list)] 

and many other things but nothing has worked. (I'm quite inexperienced with Python)

Any help would be greatly appreciated. Thanks.

Upvotes: 1

Views: 2123

Answers (8)

eyquem
eyquem

Reputation: 27585

Using no index and doing no creation of list nor sublist:

lst = [0, ['~', '~', '~'], 1, ['~', 889, '~'], 2, ['~', '~', ('a','b')], 3, ['#', '#'] ]

it = iter(lst)
print '\n'.join( '%s %s' % (x,' '.join(str(u) for u in next(it))) for x in it)

result

0 ~ ~ ~
1 ~ 889 ~
2 ~ ~ ('a', 'b')
3 # #

Upvotes: 0

Jong Bor Lee
Jong Bor Lee

Reputation: 3855

Not as clever or concise as the other answers, but definitely readable:

# This function is taken from 
# http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python
def chunks(l, n):
    """ Yield successive n-sized chunks from l.
    """
    for i in xrange(0, len(l), n):
        yield l[i:i+n]

my_list = [0, ['~', '~', '~'], 1, ['~', '~', '~'], 2, ['~', '~', '~'] ]

paired = chunks(my_list, 2)

for index, lst in paired:
    print index, ' '.join(lst)

Upvotes: 2

Fred Foo
Fred Foo

Reputation: 363627

First off, let's correct that definition:

lst = [0, ['~', '~', '~'], 1, ['~', '~', '~'], 2, ['~', '~', '~']]

Please, never use list as an identifier name. It shadows the builtin function list.

To make this easier to handle, convert it to a simple list of lists:

lst = [[lst[i]] + lst[i+1] for i in xrange(0, len(lst), 2)]

Then join that:

print "\n".join([" ".join(map(str, x)) for x in lst])

Upvotes: 1

juliomalegria
juliomalegria

Reputation: 24921

First, I'm using zip() to group your list in groups of two. Then, I'm iterating over the sublist, and using join() to join its elements with a whitespace, appeding it to the first element, and finally, joining all lines with a breakline (\n).

>>> my_list = [0, [11, 1.2, 'str1'], 1, [21, 2.2, 'str2'], 2, [31, 3.2, 'str3'] ]
>>> it = iter(my_list)
>>> my_str = '\n'.join((str(x) + ' ' + ' '.join(str(y) for y in l)) for x, l in zip(it, it))
>>> print my_str
0 11 1.2 str1
1 21 2.2 str2
2 31 3.2 str3

This solution will work with any kind of type in your list and sublists.

Upvotes: 0

jfs
jfs

Reputation: 414405

You could iterate over the list in chunks (two items at a time) using standard "grouper" idiom:

L = [0, ['~', '~', '~'], 1, ['~', '~', '~'], 2, ['~', '~', '~']]
print('\n'.join("%s %s" % (i, ' '.join(lst)) for i, lst in zip(*[iter(L)]*2)))
Output
0 ~ ~ ~
1 ~ ~ ~
2 ~ ~ ~

Upvotes: 0

ben w
ben w

Reputation: 2535

If your list really has that exact structure (non-list element in even positions, list element in odd positions) you could do this:

def pairs(l):
    i = iter(l)
    while 1:
        yield (i.next(), i.next())
print '\n'.join(['%s %s' % (f, ' '.join(s)) for f, s in pairs(list)])

Upvotes: 0

zwol
zwol

Reputation: 140679

One can always use explicit iteration by twos:

el = [0, ['~', '~', '~'], 1, ['~', '~', '~'], 2, ['~', '~', '~'] ]
for i in range(0, len(el), 2):
    print el[i], " ".join(el[i+1])

I cannot presently think of anything cleverer.

Upvotes: 6

Adam Mihalcin
Adam Mihalcin

Reputation: 14468

You could use a comprehension over the indices up to the length of the list

'\n'.join('%s %s' % (list[2 * i], list[2 * i + 1]) for i in [0:len(list)/2])

Upvotes: 0

Related Questions