Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [split] Automate the boring stuff, inserting commas in list
#1
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.
snippsat wrote Jul-02-2019, 02:09 PM:
Added code tag in your post,look at BBCode on how to use.
Quote
#2
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,']
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  "Automate the Boring Stuff with Python" creating a path works but only for CMD promt Milos 0 54 Mar-23-2020, 08:47 PM
Last Post: Milos
  [split] question about list comprehension Armin 17 539 Jan-29-2020, 04:32 PM
Last Post: Clunk_Head
  split the list SriRajesh 2 159 Jan-01-2020, 07:07 PM
Last Post: perfringo
  List help to split into single entries paul41 3 257 Nov-25-2019, 08:09 AM
Last Post: perfringo
  How to automate list separation. NOOB LobateScarp 18 659 Sep-24-2019, 07:28 PM
Last Post: LobateScarp
  Getting Cells from the Sheets "automate the boring stuff" Shafla 8 377 Sep-24-2019, 04:53 AM
Last Post: snippsat
  Help|CSV Writing on List with Inner Commas soothsayerpg 2 335 Jul-20-2019, 06:59 AM
Last Post: scidam
  Split the list and obtain a single value Gururaj 1 433 Jul-12-2019, 12:01 AM
Last Post: scidam
  Regex Issue from Automate the Boring Stuff book robgraves 2 520 Jun-16-2019, 12:41 AM
Last Post: robgraves
  Split List and Sublist from Pyodbc parthi1705 1 435 May-05-2019, 10:44 AM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)