1

Hi I have to print each element in nested list using nested loops, but the code i wrote prints each element per line, but i need it to print all the items in the inner list per line.

new_grid=[['(a)', '(b)'], ['(c)','(d)'], ['(e)', '(f)']]
def print_newgrid():
 ''' 
 when printed it should look like:
 (a)(b)
 (c)(d)
 (e)(f)
 '''
 for i in new_grid:
 for j in i:
 print(j)

This prints each element per line, instead of two. Any help is appreciated thanks

asked Feb 25, 2017 at 1:30

2 Answers 2

2

Since you say you need to use nested lists, try:

>>> for i in new_grid:
... for j in i:
... print(j,end="")
... print("")
...
(a)(b)
(c)(d)
(e)(f)

Or more simply:

>>> for i in new_grid:
... print("".join(i))
...
(a)(b)
(c)(d)
(e)(f)

Since

answered Feb 25, 2017 at 1:32
2
  • i have to use nested lists Commented Feb 25, 2017 at 1:33
  • Both of @bernie's examples assume nested lists :) Commented Feb 25, 2017 at 1:36
0

This

print "\n".join([l[0] + l[1] for l in new_grid])

Will give

(a)(b)
(c)(d)
(e)(f)
answered Feb 25, 2017 at 1:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.