FAQ: Lists - Removing Items from a List

This community-built FAQ covers the “Removing Items from 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 Removing Items from 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!

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

myList.splice(1,1)

print (myList)


myList.splice(1,1)
AttributeError: ‘list’ object has no attribute ‘splice’

This is the error I got. Does anyone knows why? Much appreciated!

It would appear you are working in a Python environment. splice is a JavaScript method. Python has no such method.

2 Likes

Hi,

I have some questions. To be able to move forward in this learning process I need to understand and be comfortable with what I have learnt. I’m completely new to this, so maybe the questions are “stupid”, but I would appreciate if somebody takes time to answer them.

  1. .pop
    In previous exercise we used .append to add something at the end of a list. Does in this case .pop mean “remove something from the end of a list”? Are we always using .pop when we want to remove something from the end of a list? What is the meaning of .pop?

  2. .splice
    Does .splice always mean that something is happening in a middle of a list? In previous exercise we were adding something with .splice, now we are removing it with .splice? How do a computer know then when we are adding and when we are removing if we in both cases use .splice?

  3. MyList.splice (1, 1)

What is (1, 1)? Does the first 1 mean “index 1”? Does the second 1 mean “remove 1 item from a list”?

  1. / / returns ‘pear’ or // returns ‘banana’ or // now, myList = [ ‘apple’, ‘pear’] etc.
    What are those grey explanations? Are we going to type them too when coding? Or are they only here as an explanation (instructions) for us, so that we understand what is happening and that they don’t really exist when coding in reality?

Thanks in advance for your answers,
Natasha

12 Likes

If this is JavaScript, then there is no .append() method for arrays that I’m aware of. There is a node insertion method, .append but deals with the DOM.

We use .push() to append, and .unshift() to prepend.

.pop() is always the last item in the array to be removed. It can be assigned or used in an argument.

a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
b = a.pop()
console.log(b)
console.log(a)
// 9
// [1, 2, 3, 4, 5, 6, 7, 8]

.splice() will mutate the array it is acting upon, and will return the values removed as an array. It can remove zero or more items, and can replace them with zero or more items starting at that index.

a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
b = a.splice(4, 2)
console.log(b)
// [5, 6]
console.log(a)
// [1, 2, 3, 4, 7, 8, 9]
a.splice(4, 0, 5, 6)
console.log(a)
// [1, 2, 3, 4, 5, 6, 7, 8, 9]

Any code preceded by // does not execute. It is a single line comment providing information to the reader.

4 Likes

Hello @nanski84 ,
@mtf has answered your questions, but I think you or maybe others need more explanation on how .splice() method works… Please, check out the below:

  1. .splice() method used for add or remove items to or from the list.
  2. inside .splice() brackets () there are three types of data .splice(--1--, --2--, "--3--", "--3--", "--3--"):
  • the first space --1-- tells what index[] number you will to start with.
  • the second space --2-- tells how many items will be removed starting from the index[]
    number given in the first space…(if second number is set to 0 means, remove nothing).
  • the third space “–3--” (between double quotation marks) are the items which will be
    added to the list.
///////////////////////////example 1
kokoGroup = ["koko00", "koko11", "koko22", "koko33", "koko44", "koko55", "koko66"]
action = kokoGroup .splice(4, 2)
console.log(action)
// this will remove two items starting from the 
//fifth item in the list ["koko44", "koko55"]
console.log(kokoGroup)
// ["koko00", "koko11", "koko22", "koko33", "koko66"]

///////////////////////////example 2
kokoGroup = ["koko00", "koko11", "koko22", "koko33", "koko44", "koko55", "koko66"]
action = kokoGroup.splice(2, 1, "ToTo-1", "ToTo-2" )
console.log(action)
// this will remove one item starting from the 
//third item in the list ["koko22"], 
//and add two extra items ["ToTo-1", "ToTo-2"]
console.log(kokoGroup )
// ["koko00", "koko11", "ToTo-1", "ToTo-2", "koko33", "koko44", "koko55", "koko66"]
26 Likes

Hi, i’m also new at programming so i don’t understand what means returns? In this exercise we have myList.pop() // returns ‘pear’ but why? would it be able to function without returns?

1 Like

It refers to the final output of the method, what is given back to the caller.

[1, 2, 3, 4, 5].pop()    #  5

5 is returned by the method.

hey, just curious about removing and adding items to a list in the examples I can use splice and append to add or remove items

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

but wouldn’t it be the same to just edit said line instead of making a new line just to add/remove
like why can’t we go back to my list and put orange inside or delete pear

1 Like

A program when running cannot be edited. That is why there are methods that can modify an array dynamically. We might not have any hand in the original creation of the list, only in how that data is read from or written to. Our intentions are written in the code, with all operations being done by that code.

The principal methods we have at our disposal are,

unshift()
shift()

for inserting at, and removing from the left side (beginning) of the array;

push()
pop()

for inserting at, and removing from the right side (end) of the array; and,

splice()

for inserting at, or removing from within the array.

Edited: Nov. 3, 2019: why

1 Like

Thak you for very comprehensive explanation. Things are starting to make sense now

why is 1 written twice in this code : myList.splice(1, 1) ?

2 Likes

When we interpret that expression it means essentially, ‘remove the one element at index 1’.

2 Likes

looking for this solution. You solved this. thanks.

1 Like

Thank you very much for the extremely helpful explanation Tobasi & MTF ! I was confused about the numbers following a .splice, and felt uncomfortable moving forward. You two broke that down perfectly, I appreciate that very much. Your comments were lessons of their own !

2 Likes

If we had written myList.splice(1,1,‘mango’) would mango replace ‘banana’ ie:
myList=[‘apple’,‘mango’,‘pear’] ?

I just want to make sure I’m understanding it correctly.

Welcome, @webcoder12367,

The second positional argument is how many elements to delete, so if ‘banana’ is in the second position in the array, then it will be deleted before insertion of the new entry. If that is the idea you have, then you are on the right track.

1 Like

what do you mean by (4,2)?

We can read up the documentation for Array.splice to get a complete picture of what the method promises to do. Basically, the first positional argument, 4 is the index of the start of the splice, and the second argument is the element count.

Array.prototype.splice() - JavaScript | MDN

1 Like

Previous lesson says do not worry about pop/splice then in very next lesson has the same concepts with a comic frame and no other exercise/instructions? What does this do? How do I learn this?