-2

I want to take a list like this

mylist = [['_','X','_'],['X','_','_'],['X','_','_']]

and display it like this:

_ X _
X _ _
X _ _

would I use 2 nested loops and build a string?

mystring=''
for line in mylist:
   for char in line:
       mystring += char + ' '
   mystring += '\n'
screen.addstr(mystring,0,0)
lol
  • 481
  • 6
  • 18
  • Possible duplicate of [Printing list elements on separated lines in Python](http://stackoverflow.com/questions/6167731/printing-list-elements-on-separated-lines-in-python) – Chris_Rands Feb 09 '17 at 18:08

2 Answers2

1

Use str.join:

mylist = [['_','X','_'],['X','_','_'],['X','_','_']]

mystring = '\n'.join(' '.join(sublist) for sublist in mylist)

screen.addstr(mystring,0,0)
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
0

You can do this using str.join :

for i in mylist :
    print ''.join(i)

If you want spaces between the characters, replace '' by a whitespace ' '.

Jarvis
  • 8,494
  • 3
  • 27
  • 58