Python Forum
[split] Automate the boring stuff, inserting commas in list
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.
Reply
#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,']
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,430 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Split string using variable found in a list japo85 2 1,238 Jul-11-2022, 08:52 AM
Last Post: japo85
  help with commas in print functions kronhamilton 11 3,348 Feb-10-2022, 02:02 PM
Last Post: mishraakash
  Run the code for some stuff it does not return me why Anldra12 3 2,798 Apr-19-2021, 02:01 PM
Last Post: Anldra12
  "Automate the Boring Stuff with Python" creating a path works but only for CMD promt Milos 2 2,822 Nov-28-2020, 01:08 PM
Last Post: Larz60+
  Unable to print stuff from while loop Nick1507 4 2,280 Sep-17-2020, 02:26 PM
Last Post: Nick1507
  How Do I Install Stuff for Python? CopBlaster 6 3,133 May-08-2020, 12:27 PM
Last Post: hussainmujtaba
  Learning to have Class and doing stuff with it... bako 2 1,958 Apr-29-2020, 05:07 PM
Last Post: bako
  [split] question about list comprehension Armin 17 5,529 Jan-29-2020, 04:32 PM
Last Post: Clunk_Head
  split the list SriRajesh 2 2,172 Jan-01-2020, 07:07 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020