Python Forum
Automate the boring stuff, inserting commas in list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Automate the boring stuff, inserting commas in list
#1
Hello!
I cannot figure out something about negative indexes in that exercise of automate the boring stuff:

"
Comma Code:

Say you have a list value like this:

spam = ['apples', 'bananas', 'tofu', 'cats']

Write a function that takes a list value as an argument and returns a string with all the items separated by a comma and a space, with and inserted before the last item. For example, passing the previous spam list to the function would return 'apples, bananas, tofu, and cats'. But your function should be able to work with any list value passed to it."
Automate the boring link

I am trying for variation to save insert commas instead.

def listing(spam):
    for i in range (1,len(spam),2):
        spam.insert(i, ' ,') #inserting a comma with an offset of 2
    spam.insert(-1, ' and ') #insert and at the second to last position, it has to be -1 and not -2, why?
    return spam
    
    
spam = ['apples', 'bananas', 'tofu', 'cats']
print(listing(spam))
The line that confuses me is 4:
spam.insert(-1, ' and ') #insert and at the second to last position
Why not inserting at -2, that is the second to last? I spent quite a while thinking about it, I can't figure out why the negative index needs to be -1. Sick
Reply
#2
spam.insert(i, 'and') inserts at index min(len(spam), i) when i is non negative and at index max(0, len(spam) + i) when i is negative.
Reply
#3
(Apr-21-2019, 04:50 AM)Gribouillis Wrote: spam.insert(i, 'and') inserts at index min(len(spam), i) when i is non negative and at index max(0, len(spam) + i) when i is negative.

Sorry, I don't get it...
Reply
#4
(Apr-21-2019, 04:17 AM)DJ_Qu Wrote: Why not inserting at -2, that is the second to last? I spent quite a while thinking about it, I can't figure out why the negative index needs to be -1. Sick

When in doubt, it's always good to start with built-in help:

>>> help(list.insert)
Help on method_descriptor:

insert(self, index, object, /)
    Insert object before index.
(END)
I think that this is quite straightforward explanation.

Why is it so? Try to answer the question: how to insert something before first element which happens to have index 0? There is no index that can represent that position... This ain't replace method which replaces element on specific index.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help with commas in print functions kronhamilton 11 3,344 Feb-10-2022, 02:02 PM
Last Post: mishraakash
  Run the code for some stuff it does not return me why Anldra12 3 2,797 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,821 Nov-28-2020, 01:08 PM
Last Post: Larz60+
  Unable to print stuff from while loop Nick1507 4 2,275 Sep-17-2020, 02:26 PM
Last Post: Nick1507
  How Do I Install Stuff for Python? CopBlaster 6 3,131 May-08-2020, 12:27 PM
Last Post: hussainmujtaba
  Learning to have Class and doing stuff with it... bako 2 1,955 Apr-29-2020, 05:07 PM
Last Post: bako
  How to automate list separation. NOOB LobateScarp 18 5,351 Sep-24-2019, 07:28 PM
Last Post: LobateScarp
  Getting Cells from the Sheets "automate the boring stuff" Shafla 8 3,926 Sep-24-2019, 04:53 AM
Last Post: snippsat
  Help|CSV Writing on List with Inner Commas soothsayerpg 2 2,311 Jul-20-2019, 06:59 AM
Last Post: scidam
  [split] Automate the boring stuff, inserting commas in list srikanth 1 2,077 Jul-02-2019, 02:29 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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