简体   繁体   中英

How to append new line to the list

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"

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(']')

This worked for me.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM