[Question]Learn Python 3 - Lists - Len's Slice

Hi everyone, I am doing Len’s Slice practice and I am coming across a difficulty, please see the screenshot below:

The result is expected to be: [[2, ‘pepperoni’], [6, ‘pineapple’], [1, ‘cheese’], [3, ‘sausage’], [2, ‘olives’], [7, ‘anchovies’], [2, ‘mushrooms’]]
But I got: [(2, ‘pepperoni’), (6, ‘pineapple’), (1, ‘cheese’), (3, ‘sausage’), (2, ‘olives’), (7, ‘anchovies’), (2, ‘mushrooms’)]

I do not understand how to get that result. Please help. Thanks in advance!

Link: https://www.codecademy.com/courses/learn-python-3/projects/python-lens-slice

4 Likes

Welcome to the forums :slight_smile:

Using list(zip(a, b)) returns a list of tuples (which are immutable lists) which is why you are getting this output.

At this point in the course though I doubt they want you to hard code everything, and since zip() returns this way I’m not sure how they want you to get a list of lists.

Not that it’s impossible, as you can use list comprehension to do it:

[list(a) for a in zip(prices, toppings)]

But I don’t believe they’ve covered this yet. I’ll ask about it, perhaps there’s a typo in the hint.

4 Likes

I think this and a few other lessons removed any introduction of tuples in a recent update (last couple of weeks), which I suppose is fine, but it broke or confused a few instructions in older lessons. There’s a bug report about it (for a specific lesson). May be worth tagging this instruction in with that group so that it’s made clearer too if the lessons are updated again (fingers crossed).

* Looks like list comps are covered in this course before this project, not sure about zip though or a more advanced comprehension like that one.

3 Likes

I actually do think they expect learners to make the 2D list by hand for this one. The hint suggests they don’t want you to use zip() since this doesn’t involve tuples (and zip has been moved to the end of the Lists module). I don’t think list comprehension is covered yet here, and even for loops aren’t.

My only other guess is that maybe they want learners to build it using .append() (which has been covered):

pizza_and_prices = []
pizza_and_prices.append([2, "pepperoni"])
...
# continue using .append() for each price/topping
2 Likes

I think it’s combining them by hand too.

.zip() is now taught after this project. (we had the same issue with the “gradebook” project after they revamped it too where we’d used .list(zip()) previously.

using .append() repeatedly is tedious. lol.

4 Likes

Yeah, the rationale in moving zip() was to place it after the tuples video and also expand the 2D list content. See this discussion on Discord.

It’s tedious, but the more I think about it, the more I’m inclined to say the curriculum team wants learners to use .append(). Otherwise, what would be the point of printing it out? If you write it by hand, it will look the same printed out as it does in script.py.

2 Likes

I understand it. They just need better copyeditors for the lessons. :wink:

2 Likes

Hi! Curriculum Dev here!

To clarify your question – we are expecting you to manually type out a 2d list here. Our new structure for the project does not need the use of zip().

Sadly, Python does not give us an easy way to form 2d lists without the use of loops (which learners may or may not have learned before this point).

We will take a look at the project to clarify this point for current and future learners. :slight_smile:

9 Likes

Hi there,
my feeling is, that the gradebook as well as the Len’s slice project are not updated yet. I got pretty much stuck in these projects since I wasn’t able to come up with a solution based on the previous learnings. Would be great, if you could be more precise with the task there.

However, a solution I found is at least not as unsexy as writing the whole 2d list manually and is pretty close to the learnings at this stage of the course so far:

pizza_and_prices = [prices[0], toppings[0]], [prices[1], toppings[1]], [prices[2], toppings[2]], [prices[3], toppings[3]], [prices[4], toppings[4]], [prices[5], toppings[5]], [prices[6], toppings[6]] pizza_and_prices = list(pizza_and_prices)

The only issue is, that the list becomes a tuple and must be converted to a list again.

Hello! I am assuming you were stuck on step 6: “Convert our toppings and prices lists into a two-dimensional list called pizza_and_prices that has the following associated values.”

Can you clarify what is confusing about step 6? Is it the usage of “convert” that is implying you need to use something like zip() or list()?

Would saying something like “Use the existing toppings and prices information to form a two-dimensional list” clarify this issue?

Hi!
Yep, I guess “convert” triggered me. Your phrase suggestion is already much better, but I would be even more precise. Since the manual creation of the new list does not gain any learnings, I would explicitly say, that this list has to be created manually. Even better would perhaps be, if you would provide the list as you did with some other part of the code in the exercises before. Part of this exercise must then be, that you have to “uncomment” the list to use it in the further tasks.
Thanks for picking this topic up again!

Hello I typed the second list but I am still getting an error
This is my code
num_two_dollar_slices = prices.count(2)
print(num_two_dollar_slices)

num_pizzas = len(list(toppings))
print(‘We sell’, num_pizzas, ‘kinds of pizza’)

pizza_and_prices = [[2, ‘pepperoni’], [6, ‘pineapple’], [1, ‘cheese’], [3, ‘sausage’], [2, ‘olives’], [7, ‘anchovies’, [2, ‘mushrooms’]]
I get a syntax error here and so cannot continue. Thank you!

check if you’re missing ] after 'anchovies'

2 Likes

That would be nice! Bc I did not get it fro mthe definition that we have to type the 2D array. And was stuck for a while. thanks.