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.

3 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)