I am trying to write a list to a file. When I use the following code it gives me an error :
with open('list.txt', 'w') as fref :
fref.writelines('{}\n'.format(item for item in item_list))
But when I modify the code to :
with open('list.txt', 'w') as fref :
for item in item_list :
fref.writelines('{}\n'.format(item))
or
when I format the string using % :
with open('list.txt', 'w') as fref :
fref.writelines('%s\n' % item for item in item_list)
it works fine. I am confused as to why does the for loop inside the format method fail ?