FAQ: Introduction to Lists in Python - Shrinking a List: Remove

This community-built FAQ covers the “Shrinking a List: Remove” exercise from the lesson “Introduction to Lists in Python”.

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

Visualize Data with Python
Data Scientist
Analyze Data with Python
Computer Science
Analyze Financial Data with Python
Build Chatbots with Python
Build Python Web Apps with Flask
Data Analyst

Learn Python 3
CS101 Livestream Series

FAQs on the exercise Shrinking a List: Remove

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!
You can also find further discussion and get answers to your questions over in Language Help.

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

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

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!

Hello everyone,

just learned about the .remove method. However, if I wanted to remove an item in a list in relation to its index, how do I go about this?
…considering the fact that .remove does not accept any parameter, other than the actual item to be removed.

2 Likes

You could use the del statement- https://docs.python.org/3/tutorial/datastructures.html#the-del-statement, del lst[index]

1 Like

Oh, thank you. This is extra, and very useful. I later found what I was looking for.

The .pop method. We can remove an item in a list in relation to it’s index. But your response taught me something new, and I appreciate!

5 Likes

Hi everyone,
is there a method to remove more than one item at a time?

Thnaks!
Lorens

with the code :

new_store_order_list = [“Orange”, “Apple”, “Mango”, “Broccoli”, “Mango”]
new_store_order_list.remove[“Mango”]

how come it only removes one of the "Mango"s and not both? And how does it decide to remove the Mango in index 2 or the Mango in index 4?

2 Likes

That’s just the way it’s designed to work, if you check the docs it gives a brief description-

Remove the first item from the list whose value is equal to x

https://docs.python.org/3/tutorial/datastructures.html#more-on-lists

Thank you! In that case, how would you go about removing all the Mangos in the list? Is the only way to use a “for” loop to look for every mango in the list?

In truth if the list was not referenced elsewhere (and didn’t consume an absurd amount of memory) I’d prefer to create a new list skipping all the values that equal ‘mango’, so-

new_store_order_list = [
    element
    for element in new_store_order_list
    if element != 'mango'
]

The problem with removing multiple values is that the length of the list changes when you remove them, so iterating through that list becomes quite difficult. I think a single pass noting the index of each matching element and then removing them in reverse might be a passable option. Sounds like an interesting task if you wanted to give it a shot :slightly_smiling_face:.

Wow thank you so much!

2 Likes

Just went and completed “Shrinking a List: Remove” and there was no requirement to iterate over the list. We use only the .remove() method to remove “Flatbread” and then later, “Mango”, and attempt to remove “Onions” to see the KeyError that gets raised.

1 Like

I like the del statement better.
I also found .pop() to delete by index.

1 Like

you can use del statement like take a example code:

my_list = ['medicine', 'groceries', 'snacks', 'toys']
      # if i want to remove snacks cause im getting fatty so i can:
del my_list[2]
  print(my_list)
#in output my snacks will be removed :( 

1 Like

del - it means like Delete?

The link in that message would give you more information(it also links again to more info) than I can reply with but yes it’s a contraction of delete.

1 Like

So today, I found two exciting ways to remove an index.

del new_store_order_list[-1]
new_store_order_list.pop(-1)

I think .pop is my favorite.

Hi everyone, quick question:
For the exercise provide to remove “Mango”, why did it choose to remove the middle position “Mango” instead of the Mango at the last index? Is there a hidden rule behind it?

Thanks for answering!

If you scroll further up :arrow_up: and read previous posts, you will see an answer to your question (which was asked before).

You could also consult the documentation:

1 Like

Oh ok I have read the previous posts. Thank you very much!

1 Like

How come the remove() doesn’t remove both “Chris” indexes in the example?

Is there a way to remove all indexes of a particular string, boolean, int, or float, at the same time?

Also, I do not know how to copy and paste from the example like I’ve seen others do. I tried to do it here and part of my message was formatted as a header and it didn’t look right. How do we copy and paste the example exercises? Thanks!