Python: for loop inside print()

Viewed 78573

I have a question about Python (3.3.2).

I have a list:

L = [['some'], ['lists'], ['here']] 

I want to print these nested lists (each one on a new line) using the print() function:

print('The lists are:', for list in L: print(list, '\n'))

I know this is incorrect but I hope you get the idea. Could you please tell me if this is possible? If yes, how?

I know that I could do this:

for list in L:
    print(list)

However, I'd like to know if there are other options as well.

5 Answers

work for me:

L = [['some'], ['lists'], ['here']]
print("\n".join('%s'%item for item in L))

this one also work:

L = [['some'], ['lists'], ['here']]
print("\n".join(str(item) for item in L))

this one also:

L = [['some'], ['lists'], ['here']]
print("\n".join([str(item) for item in L]))

If anyone looking for help in dict printing then here u go:

dictionary = {'test1':'value1', 'test1':'value1'}


print ('\n'.join(' : '.join(b for b in a) for a in dictionary.items()))

Came late, but you can simply do

print("The lists are:", [list for list in L])
Related