FAQ: Lists - Adding Items to a List

Welcome to the forums!

1 and 0 are arguments that you’re passing to the .splice() method. You are passing 1 to the start parameter and 0 to the deleteCount parameter. 1, in this case, is the index at which to start changing the array. 0, in this case, is the number of elements in the array we want to remove, starting at start (in this case, index 1).

You can read more about .splice() here.

Hi

What I don’t understad is why we need to add an item this way (mylist.append), and not by simply editing the “mylist” sentences?

We could edit the elements inside my_list by rewriting it like so:

myList = ['apple', 'banana', 'pear']
myList = ['apple', 'banana', 'pear', 'orange']

However, this would be incredibly inefficient. Imagine if our list had more than just a few elements. What if it had dozens? Or hundreds? .append() allows us to add an element on to the end of a list and saves us the effort needed to rewrite the list each time we want to modify it.

1 Like

Editing requires access to the source code. Changes made that way are referred to as maintenance done on the physical source file(s), even if just to add new default data points. This is impossible during runtime. We need our program to dynamically modify the data given new user inputs, state changes, etc. That’s why we have list methods inherited from the parent class that give us the ability to mutate values, add data to and remove it from the list. The data is dynamic and changes are made on the fly, by our program, not us.

Methods you will become familiar with are, .append(), .extend(), .insert(), .remove(), .pop(), and .sort(), among others. They are runtime methods, not maintenance tools.

1 Like

myList = [‘apple’, ‘banana’, ‘pear’]

myList.splice(1, 0, ‘mango’) // make ‘mango’ the second item in the list

// now, myList = [‘apple’, ‘mango’, ‘banana’, ‘pear’]

Given the above information, is it possible for the argument to be something like

myList.splice(1,‘mango’) // thereby simply inserting Mango in that position? or is a second number necessary??

If a second number is necessary then that would mean that you are simply telling telling the line of command where to put the current “fruit” aka between the current index of 1 and 0, thereby becoming 1.

Am I on the right track?

If that means free of assumptions, then yes. However, it is always a good idea to check with the documentation.

The first positional argument is the insertion index, meaning the value at that index will be moved to the right to free up the space.

The second positional argument is the deletion count (how many items to delete starting at the insertion index). Check what the default is, if there is one. Does the second argument default to zero when it is omitted?

The third positional argument is the item to insert. How many items can there be? How are they to be given?

On the understanding that we can insert multiple, comma separated items, that would mean the item at index 1, would be moved far enough to the right to permit complete insertion of the items, assuming there are no deletions.

Be sure to read up on this in the docs. Scroll up to the reply from 3 Feb '21 for a link to the MDN site.

1 Like

So if we used the command

myList.splice(1,‘mango’)

That is a valid splice because we are not wanting to delete anything before or after it right?

and if we used

myList.splice(1,‘mango’, ‘dragon fruit’, ‘nuts’)

That would then insert all 3 of them in that order, starting at index value 1.

It depends how we interpret this,

If deleteCount is omitted, or if its value is equal to or larger than array.length - start (that is, if it is equal to or greater than the number of elements left in the array, starting at start ), then all the elements from start to the end of the array will be deleted.

The way I read this, everything from index 1 and beyond will be deleted, and replaced by the four items given. This means we should specify 0, for no deletions, or some positive number for the number deletions.

In a workspace, try experimenting to see what happens, and document your findings with examples and outcomes.

Ok, I think I understand now.

we use the 1 as the index we want to place a value in, and the second number (0), is necessary to state how much to delete after the inserted value is set into place.

If nothing is set into place, such as (1,‘Mango’) then only the index 0 and 1 are left and everything after index 1 is deleted, because we are stating everything after index 1 is to be deleted.

That is how I interpret it. Have you tried some experiments to test your hypothesis? Set up a number of examples and record them in a screenshot. Of course, please share the outcomes.

1 Like

You said that if the number 0 was 1 instead, the order will start with “banana” instead of apple… But, following this, if you insert something, in this case, mango, at “0” index, wouldn’t the list start with “mango”, “apple”; "banana; etc… ??? Assuming the second argument is 0 as well (not deleting)?

OMG that just made it make sense. Thanks!

1 Like

Thanks a million for understanding what made us so quizzical about altering lists with splice commands and, further, how to go ahead and show what removing with the second argument can look like, in more than one instance (with more than one objective vis a vis placement, that is). This is great.

1 Like

How do I interact with the cartoons on the right? Is this exercise done in my head?