[split] Automate the boring stuff, inserting commas in list - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: [split] Automate the boring stuff, inserting commas in list (/thread-19505.html) |
[split] Automate the boring stuff, inserting commas in list - srikanth - Jul-02-2019 def sri(z): ... for i in range(len(z)): ... if i<len(z)-1: ... y=z[i]+',' ... else: ... y='and ' + z[-1] ... print(y,end=" ") ... >>> d=['apple','banana','tofu','cat'] >>> p=sri(d)I was unable to get the apostrophe on the final output. I've tried by calling str function for the final output. RE: [split] Automate the boring stuff, inserting commas in list - metulburr - Jul-02-2019 What are you trying to do. Put a comma at the end of each element? >>> [f'{element},' for element in d] ['apple,', 'banana,', 'tofu,', 'cat,'] |