FAQ: Introduction to Lists in Python - Modifying List Elements

This community-built FAQ covers the “Modifying List Elements” 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 Modifying List Elements

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!

I wanted to know, what can be done if we want to remove the single quotation marks (’ '), in the names of the people, i.e., in the strings that we enter when we print the list.

We would never remove the quotes, else they will no longer be strings. When we print, the quotes are not displayed, but printing doesn’t remove them. The strings remain intact.

How do I remove just the name and not replace it with anything else? I tried to leave it as “” but it still show up as an empty string on the list [ ‘’ ‘’]

list.remove() will remove an element with no replacement.

del lst[index] will also work, I believe.

lst = [1,2,3,4,5,6,7,8,9]
lst.remove(5)
lst
[1, 2, 3, 4, 6, 7, 8, 9]
del lst[6]
lst
[1, 2, 3, 4, 6, 7, 9]

Note that .remove() takes a value, while the other takes an index.

1 Like

Hi,

In the lists module, an exercise asks us to change the value of an index to another value through reassignment.

garden_waitlist = ["Jiho", "Adam", "Sonny", "Alisha"]
garden_waitlist[1] = "Calla"

However, I wanted to iterate through the list, find the value we should change, and then do so through a conditional statement.

garden_waitlist = ["Jiho", "Adam", "Sonny", "Alisha"]

for person in garden_waitlist:
  if person == "Adam":
    person = "Calla"

print(garden_waitlist)

And although the above script worked by passing the tests, it didn’t print out the list with the updated value. It showed the old value, the one right before reassignment.

The different approach shown below did change the value, with the added benefit of printing out the correct, updated list.

garden_waitlist = ["Jiho", "Adam", "Sonny", "Alisha"]

i = 0
while i < len(garden_waitlist):
    if garden_waitlist[i] == "Adam":
        garden_waitlist[i] = "Calla"    
    i += 1

print(garden_waitlist)

Why is this? The first method after the reassignment example should have printed the list with the updated value, no? This isn’t “asynchronous” code, meaning I shouldn’t have to “await” for the loop to complete before printing it out. Python interprets lines as the document flows, top to bottom. So, is there any idea as to what is going on here? Thank you, and kind regards,

Toby

The above is the signature line for a read-only loop. While you may have given the person variable a new value, it is transient and gets immediately overwritten by the next value in the list.

The only way to mutate the values in a list is by their index. That means the first solution is the correct (but not the only) one.

Along the way you will learn about the enumerate function, likely in a unit on iterators, of which an enumerate object is one. Without going into details, just follow with the usage, below:

for i, x in enumerate(garden_waitlist):
    if x == "Adam":
        garden_waitlist[i] = "Calla"
>>> garden_waitlist
['Jiho', 'Calla', 'Sonny', 'Alisha']

Notice there are two iteration variables, one for the index (i) and one for the value (x).

When you do get to the unit on iterators, include this one in your reading, even if it doesn’t come up in the lessons. You will be able to apply what you learn about iterators to this one, as well.

One last thing, do not store the iterator in a list or it will take up memory. In our usage above it gets consumed in the loop, which is what we want. The original list is unaffected.

2 Likes

Dear mtf,

Thank you for the informative answer. I’ll be on the lookout for the unit on iterators.

Kind regards, Tobes

1 Like