FAQ: Lists - Adding Items to a List


#1

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!


#2

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 ?


#3

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


#4

Aha! thank you for explaining that.


#5

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?


#6

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.


#7

ok. thanks. I got it. :handshake:


#8

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?


#9

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.