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
  Commas issue in variable ddahlman 6 2,599 Apr-05-2024, 03:45 PM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 3,868 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Split string using variable found in a list japo85 2 2,684 Jul-11-2022, 08:52 AM
Last Post: japo85
  help with commas in print functions kronhamilton 11 8,835 Feb-10-2022, 02:02 PM
Last Post: mishraakash
  Run the code for some stuff it does not return me why Anldra12 3 4,482 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 4,442 Nov-28-2020, 01:08 PM
Last Post: Larz60+
  Unable to print stuff from while loop Nick1507 4 4,120 Sep-17-2020, 02:26 PM
Last Post: Nick1507
  How Do I Install Stuff for Python? CopBlaster 6 6,174 May-08-2020, 12:27 PM
Last Post: hussainmujtaba
  Learning to have Class and doing stuff with it... bako 2 3,217 Apr-29-2020, 05:07 PM
Last Post: bako
  [split] question about list comprehension Armin 17 9,959 Jan-29-2020, 04:32 PM
Last Post: Clunk_Head

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.