Python Forum

Full Version: [split] Automate the boring stuff, inserting commas in list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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,']