简体   繁体   中英

Print list in new line for each list in list in Python

I have a list in list that look like this

[['a', 9] , ['b', 1] , ['c', 4] , . . ., ['z', 2]]

From here, I want to print out each list in the list line by line to look like this

['a',9]
['b',1]
. 
. 
['z',2]

I knew that I can just use loop to print it out like this:

for i in list:
   print(i)

but my question here is that, can it be done without using loop? Like just a one line that print this without interacting with loop. (I plan to use with some alert function so I want it to be in one large message containing all list , not multiple message with one list each)

I have tried this:

print(list, sep='\n')

but it doesn't separate into one line per list. I also tried this:

print('\n'.join(list))

and this error occured:

TypeError: sequence item 0: expected str instance, list found

which seem not to work with a list in list. Any idea?

lst = [['a', 9] , ['b', 1] , ['c', 4]]

print(*lst, sep='\n')

Prints:

['a', 9]
['b', 1]
['c', 4]

You were almost there, just add some list comprehension:

my_list = [['a', 9] , ['b', 1] , ['c', 4] , . . ., ['z', 2]]
print('\n'.join(str(el) for el in my_list ))

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