Home > Blockchain >  How to append new line to the list
How to append new line to the list

Time:12-08

I'm trying to present dimensional list in one column. I'm doing it in function so I want to append everything to new list. There is a problem. I cannot append new line to the list. I cannot use join because I' am operating on the lists not string so I got the error.My output should be the same as code :

a = [[1], [2], [3]]
for elem in a:
    print(elem)
    print(" ")
Out =
[1]
 
[2]
 
[3]

The idea is that it should be in list. I want to get list and after that return it in the function(This is only a small part of code but necessary to solve my exercise). My desire ouput is to get list for example named b.

b = [[1]

[2]

[3]]

After that return in the function. I cannot use this solution because I got the wrong ouput.

def exerc():
    a = [[1], [2], [3]]
    for elem in a:
        print(elem)
        print(" ")
print(exerc())

Out = [1]

[2]

[3]

None I dont want to have this "none"

CodePudding user response:

Based on what's in your question, this will do what you're looking for:

a = [[1], [2], [3]]
print('[')
for elem in a:
    print(elem)
    print(" ")
print(']')

CodePudding user response:

This worked for me.

print(f"[{elem}]", end=" ")
  • Related