0

List 'a' can be printed as follows (CODE1):

a = [[4, 5], [2, 6]] 
print(*a, sep='\n')

The output (OP1) is:

[4, 5]
[2, 6]

I want the sublists to be printed in tab separated form. This can be done using loop as follows (CODE2):

for b in a:
print(*b, sep='\t')

Its output (OP2) is:

4   5
2   6

Can I get OP2 by modifying CODE1? I think list comprehension would be one of the routes of achieving this.


Questions referred

Neeraj Hanumante
  • 1,575
  • 2
  • 18
  • 37

1 Answers1

3

You can use str.join with generator expressions:

print('\n'.join(' '.join(str(i) for i in l) for l in a))

This outputs:

4 5
2 6
blhsing
  • 91,368
  • 6
  • 71
  • 106