FAQ: Creating and Modifying a List in Python - Review

Your code is below:

first_names=[“Ainsley”,“Ben”,“Chani”,“Depak”]

preferred_size=[“Small”,“Large”,“Medium”]

preferred_size.append(“Medium”)

print(preferred_size)

customer_data=[[“Ainsley”,“Small”,True],[“Ben”,“Large”,False],[“Chani”,“Medium”,True],[“Depak”,“Medium”,False]]

print(customer_data)

customer_data[2][2]= False

customer_data[1].remove(“Ben”)

customer_data_final= customer_data+ [[“Amit”, “Large”, True], [“Karim”, “X-Large”, False]]

print(customer_data_final)

why is it showing an error please help.

"Ben" reached out to Maria asking to remove his shipping option because he is not sure what type he wants.

Use the .remove() method to delete the shipping value from the sublist that contains ben’s data.

In your code, you have removed Ben’s name from his order
customer_data[1].remove(“Ben”)

The instructions (see bold part) want us to remove Ben’s shipping preference i.e. the True/False value in his entry. His name and size aren’t to be removed, just his shipping preference.

I’m taking this lesson right now and was having the same doubt, I’m glad I found your post!

From what I understood, when you use customer_data[1][-1].remove(False), the program reads the False specifically/separately, hence interpreting it as a boolean. When you use customer_data[1].remove(False), the program reads the False in the context of a list, [1], so it is not interpreted as a boolean, but as a list value.

customer_data is a list of lists.

customer_data[1] targets the second list in customer_data which is ["Ben", "Large", False]

Therefore customer_data[1].remove(False) is evaluated as ["Ben", "Large", False].remove(False), The remove method can be applied to a list and consequently False is removed from the list.

customer_data[1][-1] targets the last element of the second list in customer_data.

Therefore customer_data[1][-1].remove(False) is evaluated as False.remove(False), but the boolean False doesn’t have a remove method. Hence the error.

4 Likes

That is great advice, but just a bit confusing for us noobs who are just starting out and only know what we have learned in the course so far. Even the hint that they give us for this, would lead you to believe that the answer would have been

customer_data[1][2].remove(False)
OR
customer_data[1][-1].remove(False)
1 Like

See the post by @mtrtmk above. Note that he points out the second subscript will point to an element inside the object at some index. Given that the outer list consists of lists, in order to use the .remove() method we can only choose one subscript, and not two. The second subscript is pointing to an object inside the selected object. Those objects are strings or booleans, neither of which object have a .remove() method.

Also, as a side note, a lot of lists are dynamic and may grow in length. We cannot assume that the second, or third, or fourth element is the last in the list, so must target that element by direct index (from the left side) not the indirect index (from the right side).

Neither of those are correct.

customer_data[1].remove(False)

and not,

customer_data[-1].remove(False)

Granted they both refer to list objects and both have the .remove() method, but we cannot be sure that [-1] points to the second element in the outer list.

To augment the earlier ‘advice’, every ‘noob’ must take responsibility for their own learning. That means going through the course at one’s own pace without putting a time deadline on completion of a module. Any concept that raises a brow (a question or some confusion) should signal a pause in progress during which time the learner can dive further into the concept with some practical and experimental examples gleaned from readings elsewhere, not just the course/lesson narrative.

1 Like

Thanks! I don’t know how I missed @mtrtmk explanation…I must have overlooked it.
On a side note, I do agree with you when you say

Which I do. Part of what even makes that difficult is a lot of what you find, even if it seems like a beginner/noob question, tends to have the more advanced explanations and/or concepts which I have not been exposed too before and I tend to get lost in researching “well, what does that mean”. Slow and steady :slight_smile:

3 Likes


Hey all,

I’ve been facing this issue for a minute and can’t quite figure it out. My apologies for having to come to you here. I concatenated the list in the final step of this exercise but you can see in the screenshot that it’s saying I did not complete the task correctly and the explanation given is" Your value for customer_data_final does not match! Did you add the new 2d list to customer_data ?

I believe I did add the 2d list to customer_data, and it appears to be working correctly to the right. What am I missing?

https://discuss.codecademy.com/t/faq-creating-and-modifying-a-list-in-python-review/372932/48

I’m not at all trying to be rude here, so I apologize if it comes across that way, but I don’t see the solve when I click your link. Or even advice on how to come to the solution. Could you provide more context, please?

You have the same mistake as mentioned in the linked post. Even though, your screenshot shows that you have cleared step 6. the mistake lies in your code for step 6. Specifically, the line

customer_data[1].remove("Ben")

Because of this line, the second element in the output for step 7 is ["Large", False]

Step 6 didn’t want us to remove the name “Ben”. It wanted us to remove the shipping preference from Ben’s data i.e. it expected us to modify the element to ["Ben", "Large"].

Fantastic, thank you!

1 Like

first_names=[“Ainsley”, “Ben”, “Chani”, “Depak”]

preferred_size= [“Small”, “Large”, “Medium”]

preferred_size.append(“Medium”)

print(preferred_size)

customer_data=[[“Ainsley”, “Small”, True], [“Ben”, “Large”, False], [“Chani”, “Medium”, True], [“Depak”, “Medium”, False]]

print(customer_data)

customer_data[2][2]= False

customer_data[1].remove(False)

print(customer_data)

i wish i could show my creativity in this instead of being forced to make code based on what it tells me too, which i don’t see helpful in the real world. if you ‘forget’ as the script tells you, to add in an item to the list and tells you to use .append(), in the real world you’d just put that item in the list, if you were not the programmer, there would be the .append() function in the program with the argument being the user’s input. i don’t understand why i have to program exactly what the exercise says and not just make something to show off the functions/skills i learned, i don’t like to be forced to do things, especially if it’s scripted like this, my mind programs things in a different way and my imagination is several orders of magnitude higher than what this assumes. i have previous knowledge of coding but this is a new language to me, so i like to go farther with these reviews and projects than it thinks i can. the stupid long variable names are also getting annoying, even more annoying is when it says i failed when i forget to capitalize a name or something else it wants me to capitalize.

@thedispatcher while I can sympathize with your sentiment, I think to be fair this path and lesson is for absolute beginners and the person can have zero programming experience, hence the extensive handholding meant to help practise those skills. As evidenced by the questions asked in this forum topic, even arrays and lists can come across as quite complicated for some.

I haven’t checked out some of the more advanced courses yet, but it is possible that you can jump skip ahead to those if you already have other programming knowledge. This course is really teaching the basic basics. You can always just use Python reference to help you brush up the basics while tackling tougher problems.

really?
i’m all for that if it doesn’t skip any python functions i don’t know about, i know the concepts, i don’t know the language. something i would suggest to the devs, add something (not sure what, didn’t think of the name) that asks you what you know, asking questions about prior knowledge about things like the language, coding in general, just a sort of ‘what do you know’ that allows you to skip certain parts of the course related to say, for example you answered that you have lots of knowledge about coding and other languages, but are new to python, it would skip concepts and just get you through just learning the functions and what they do, this is just a quickly thought of feature, didn’t think very deep on this, but that’s the idea.

Hi, I’m having trouble understanding the numbering of indexes in the list.

For example in this exercise the data is-

customer_data = [[“Ainsley”, “Small”,True], [“Ben”, “Large”, False], [“Chani”, “Medium”, True], [“Depak”, “Medium”, False]]

The exercise is -“Chani”` reached out to Maria. She requested to switch to regular shipping to save some money.

Change the data value for "Chani"‘s shipping preference to False in our two-dimensional list to reflect the change.

In my head, I would enter customer_data[3][3] = False

But the correct answer is actually customer_data[2][2] = False

Where is the [2][2] coming from?

Python lists are zero-indexed i.e. the first element is at index 0, the second element is at index 1 and so on.

myList = ["a", "b", "c"]

print(myList[0]) # "a"
print(myList[1]) # "b"
print(myList[2]) # "c"

myString = "Hello"

print(myString[0]) # "H"
print(myString[4]) # "o"
1 Like

Ahh ok. Makes sense. Thank you!

I am making progress and trying to take what tools I have to figure out why I cannot access multiple variables in a 2D list at the same time. Below is some silly code I wrote just to see if I could figure out my own answer, but the method still eludes me. I have gone to documentation basics of Lists, but still haven’t found the answer. I am SURE it exists somewhere, but I am going to move on with my practice and see if someone is willing to help me understand why my problem exists.

All that out of the way…How can I remove the boolean values in each sublist, at index 3 all in the same line of code. I have tried several different ways that I can think of, and I feel as though I am missing a simple tool. Example below:

fellowship_data = [["Aragorn", "Medium", True], ["Legolas", "Small", False], ["Gimly", "Large", True], ["Gandalf", "X-Large", False]] fellowship_data[0:3][2].remove(True) print(fellowship_data) >>>[["Aragorn", "Medium", True], ["Legolas", "Small", False], ["Gimli", "Large"], ["Gandalf", "X-Large", False]]

I believe that should remove the Aragorn sublist’s True value as well, but I am clearly wrong. I have tried changing the slice length among other methods. When I change the .remove() from True to False I also get a ValueError telling me that “x is not in the list”…which is weird to me since it is clearly executing some could for the True value in the Gimli sublist that I do not understand.
If this is confusing and I haven’t explained it well let me know, I have everything I have tried saved in my IDLE shell and other interpreter. And I will happily reply with more of my trials for this obviously simple problem I have haha. Thanks, I appreciate anyone’s insight!