Skip to content Skip to sidebar Skip to footer

Join List Of Lists The Proper Way

I don't know if I am stupid but I have a small issue. I have a list list1 of lists. Like you can see below the list items in this list are ints. In a simple for loop I can loop thr

Solution 1:

Instead of converting the whole sublist to a string, you need to convert each item in each sublist to a string before joining, e.g.

>>>list1 = [ [0, 1], [2], [3], [4,5,6,7], [8,9], [10], [11,12] ]>>>for sublist in list1:...print' '.join(map(str, sublist))... 
0 1
2
3
4 5 6 7
8 9
10
11 12

Solution 2:

Do you just want, e.g.:

for item in list1:
    print" ".join(map(str, item))

In Python 3.x, you can do:

for item in list1:
    print(*item)

Solution 3:

I want to print a line later on that looks like "This is 0 and 1".

list1 = [ [0, 1], [2], [3], [4,5,6,7], [8,9], [10], [11,12] ]

for item in list1:
    print ('This is {}.'.format(' and '.join(str(x) for x in item)))

Post a Comment for "Join List Of Lists The Proper Way"