Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

I want to print a list, for instance:

x = [ [1, 2, 3],

      [4, 5, 6],

      [7, 8, 9],

      [10, 11, 12]]

in a certain pattern like below:

1 2 3

4 5 6

7 8 9

10 11 12

but I couldn't figure it out.

In Java, I know that a simple nested for loop can do the job but not in Python. This is what I tried in Python:

for i in range(0, 4):

    for j in range(0, 3):

        print(x[i][j])

But the above code just prints each number in a new line.

1 Answer

0 votes
by (107k points)

You just need to add one argument named end and in that just pass space:

for i in range(0, 4):

    for j in range(0, 3):

        print(x[i][j], end=" ")

    print()

Kick-start your career in Python with the perfect Python online Course now!

 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Sep 27, 2019 in Python by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...