Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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('attendees1.csv')
csv_f = csv.reader(f)

attendee_details = []

for row in csv_f:
    attendee_details.append(row[0:4])



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

print (sortedlist)
like image 373
pytonbeginner Avatar asked Mar 13 '26 15:03

pytonbeginner


2 Answers

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

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

like image 109
timgeb Avatar answered Mar 16 '26 03:03

timgeb


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.

like image 32
TigerhawkT3 Avatar answered Mar 16 '26 05:03

TigerhawkT3