FAQ: Lists - Adding Items to a List

This community-built FAQ covers the “Adding Items to a List” exercise from the lesson “Lists”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn How to Code

FAQs on the exercise Adding Items to a List

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why is the .splice command string (1, 0, ‘mango’) instead of (0, ‘mango’, 1) if putting ‘mango’ in the second position? why is the 1 before the 0 ?

9 Likes

The second position in the array is at index 1, which is the start of the splice sequence. The second argument is optional but it represents the number of elements to be deleted, in this case none.

Array.prototype.splice

14 Likes

Aha! thank you for explaining that.

1 Like

could you explain your answer in another way? I think I still don’t understand… so the zero argument represents that no items are being deleted, correct?

so if we were making ‘mango’ the third item on the list the command would read:

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

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

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

correct? how would the command look if we were deleting ‘pear’? like this?

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

myList.splice(2, 3, ‘mango’) // make ‘mango’ the third item in the list

myList.splice(2, 3, ‘mango’) // returns ‘pear’

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

correct?

2 Likes

Given that only pear exists at the original index 2, and we are inserting at that index, to delete pear we give a deletion count of 1. Recall that the count has no connection to indices.

4 Likes

ok. thanks. I got it. :handshake:

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’]

I’ve read the above but I still don’t get it.
Why can’t I put:
myList.splice(1, ‘mango’) to get mango at Index 1 position?

How does deleting work in this context?

Did you try it? As I understand it if we leave off the second argument, JS inserts its own default value in its place, 0. Check to be sure this is in fact the case.

Deleting allows us to replace the value at a given index, or a group of values beginning at that index. If we don’t delete anything, the list shifts to the right to permit insertion of the new item at that index.

I still dont get it. When: myList = [‘apple’, ‘banana’, ‘pear’]
myList.splice(1, 0, ‘mango’)
Isn’t it supposed to be calling to index 1, which inside the array it is the position 1, so it should be apple, but as an index, index 1 it should recall to banana, which is the actual index 1, instead of apple.
Im kinda confused about this exercise tho.
Some help, or a way to be able to understand whi the splice goes with 1 instead of 0, which it would be the index value of apple inside the array by position?

Nvm i just understood the one means where we are putting the mango string inside the array, which is index 1, which is after the index 0 which is apple. And the optional value of 0 mens not removing anything inside the currently values inside the array.
Yep
Got it
Not a big deal.

1 Like

Cant we add just by writing into the main list once it is created.

Like

Mylist = [‘apple’, ‘banana’,‘orange’]

Instead of using mylist.append or mylist.splice tool,

we can directly add into the main list wherever we want.

mylist = [‘apple’, ‘mango’, ‘banana’, ‘orange’]

Correct me if i am wrong or where this method will help us?

Yes, we could, but it would be counterproductive. We are given a list, and must assume that the program will do the manipulation. We cannot access the list to make manual changes while the program is running. That is why list methods are available for the purpose of mutating the list.

1 Like

Hi! I don’t understand…Could you explain please and translate ? WHY O???

??? SOS could you explain why ‘O’ ( why items to be deleted !!?) and why mixing index number and names…This chapter is very dark

Hello.
Let’s see if I got this clear:

“myList.splice(1, 0, ‘mango’)”

Number 1 = It means the new position for the “mango” string keeping the normal order of the other items and moving “banana” one position forward. If the number was 0 there instead 1, the order will start with “banana” instead “apple”.

Number 0= It means that none item in the list is gonna be deleted.

Correct?

Pretty much, yes. The first argument is the insertion point, so mango will take index position 1, and every element from there will be shifted to the right. The second argument is the deletion count, as in how many items will be replaced (if any) by the inserted slice.

1 Like