Skip to content Skip to sidebar Skip to footer

Print A Python List Line By Line

How can I get the results of my print onto new lines? The list being printed is a first name, surname and email address, so I want each one on a new line. import csv f = open('att

Solution 1:

print('\n'.join(map(str, sortedlist)))

This will work for all lists. If the list already consists of strings, the map is not needed.

Solution 2:

Since you're using Python 3, you can also do this:

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

This unpacks the list and sends each element as an argument to print(), with '\n' as a separator.

Solution 3:

Use a for loop. ie, replace the last two lines with this

for i insorted(attendee_details, key=lambda x:x[0]):
    print(i)

Note that

sortedlist =[sorted(attendee_details, key=lambda x:x[0])]

line of code will create list of list.

Solution 4:

The Simplest way would be as follows:

print(*sorted(runnerup), sep='\n')

Post a Comment for "Print A Python List Line By Line"