Print list of lists in separate lines
Asked Answered
L

10

25

I have a list of lists:

a = [[1, 3, 4], [2, 5, 7]]

I want the output in the following format:

1 3 4
2 5 7

I have tried it the following way , but the outputs are not in the desired way:

for i in a:
    for j in i:
        print(j, sep=' ')

Outputs:

1
3
4
2
5
7

While changing the print call to use end instead:

for i in a:
    for j in i:
        print(j, end = ' ')

Outputs:

1 3 4 2 5 7

Any ideas?

Lelalelah answered 10/8, 2016 at 11:35 Comment(0)
T
33

Iterate through every sub-list in your original list and unpack it in the print call with *:

a = [[1, 3, 4], [2, 5, 7]]
for s in a:
    print(*s)

The separation is by default set to ' ' so there's no need to explicitly provide it. This prints:

1 3 4
2 5 7

In your approach you were iterating for every element in every sub-list and printing that individually. By using print(*s) you unpack the list inside the print call, this essentially translates to:

print(1, 3, 4)  # for s = [1, 3, 4]
print(2, 5, 7)  # for s = [2, 5, 7]
Thiamine answered 10/8, 2016 at 11:43 Comment(1)
How would you print the transpose of this without libraries?Anthonyanthophore
I
16

oneliner:

print('\n'.join(' '.join(map(str,sl)) for sl in l))

explanation:
you can convert list into str by using join function:

l = ['1','2','3']
' '.join(l) # will give you a next string: '1 2 3'
'.'.join(l) # and it will give you '1.2.3'

so, if you want linebreaks you should use new line symbol.
But join accepts only list of strings. For converting list of things to list of strings, you can apply str function for each item in list:

l = [1,2,3]
' '.join(map(str, l)) # will return string '1 2 3'

And we apply this construction for each sublist sl in list l

Incorporated answered 10/8, 2016 at 11:45 Comment(0)
D
3

You can do this:

>>> lst = [[1, 3, 4], [2, 5, 7]]
>>> for sublst in lst:
...     for item in sublst:
...             print item,        # note the ending ','
...     print                      # print a newline
... 
1 3 4
2 5 7
Divest answered 10/8, 2016 at 12:9 Comment(0)
A
1
def print_list(s):
    for i in range(len(s)):
        if isinstance(s[i],list):
            k=s[i]
            print_list(k)
        else:
            print(s[i])
            
s=[[1,2,[3,4,[5,6]],7,8]]
print_list(s)

you could enter lists within lists within lists ..... and yet everything will be printed as u expect it to be.

Output:

1
2
3
4
5
6
7
8
Annulose answered 18/1, 2018 at 17:27 Comment(1)
Please read the question again since this answer does not solve the issue of the question.Abreact
O
1

I believe this is pretty simple:

a = [[1, 3, 4], [2, 5, 7]]   # defines the list
    
for i in range(len(a)):      # runs for every "sub-list" in a
    for j in a[i]:           # gives j the value of each item in a[i]
        print(j, end=" ")    # prints j without going to a new line
    print()                  # creates a new line after each list-in-list prints

output

1 3 4 
2 5 7 
Opposable answered 5/6, 2020 at 17:38 Comment(0)
B
1
a = [[1, 3, 4], [2, 5, 7]]
for i in a:
    for j in i:
        print(j, end = ' ')
    print('',sep='\n')

output:

1 3 4
2 5 7
Bannockburn answered 10/7, 2020 at 18:11 Comment(2)
Pls add some description to your answer and explain how it works and solves the problem.Alister
Your last print is quite confusing for no reason. print('',sep='\n') == print()...Lukas
O
1

There is an alternative method to display list rather than arranging them in sub-list:

tick_tack_display = ['1', '2', '3', '4', '5', '6', '7', '8', '9']

loop_start = 0
count = 0
while loop_start < len(tick_tack_display):
     print(tick_tack_display[loop_start], end = '')
     count +=1
     if count - 3 == 0:
       print("\n")
       count = 0
     loop_start += 1

OUTPUT :

123
456
789
Ong answered 24/12, 2020 at 21:1 Comment(0)
M
1
lst = [[1, 3, 4], [2, 5, 7]]
 
def f(lst ):
    yield from lst


for x in f(lst):
    print(*x) 

using "yield from"...

Produces Output

1 3 4
2 5 7

[Program finished] 
Maracanda answered 19/2, 2021 at 4:6 Comment(0)
W
1

There's an easier one-liner way:

a = [[1, 3, 4], [2, 5, 7]] # your data
[print(*x) for x in a][0] # one-line print

And the result will be as you want it:

1 3 4
2 5 7

Make sure you add the [0] to the end of the list comprehension, otherwise, the last line would be a list of None values equal to the length of your list.

Whirlybird answered 12/5, 2021 at 2:32 Comment(0)
U
0

I guess the easiest way will be:

a = [[1, 3, 4], [2, 5, 7]]
for i in a:
  for j in i:
      print(j, end = " ")
  print('\n')
Urceolate answered 29/12, 2023 at 1:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.