Search code examples
pythonlistprintingnested

printing a nested list without brackets and commas work, python


maze = [["|"," ","|"],[" "," "," "],[" ","_","|"],
          ["|"," "," "],[" "," "," "],[" ","_","|"],
          ["|"," ","|"],[" "," "," "],[" ","X","|"]]

How do I print the given list in a such a way that the commas ',' and the brackets '[]' are separated, and there is a new line every three elements. I've tried the code given below, but it doesn't work.

  for x in maze:
      print('\n'.join(map(int,maze)))

I want the output to be like:

| | _|  
|   _|
| |  | 

basically, it's a maze without the upper and lower roofs

At the same time, How should I represent the upper and lover borders of the maze? Thanks


Solution

  • counter=0
    for lst in maze:
        for item in lst:
            print(item,end="")
        counter+=1
        if(counter%3==0):
            print()
    

    or use join method:

    counter=0
    for item in maze:
        print("".join(item),end="")
        counter+=1
        if(counter%3==0):
             print()
    

    have fun :)