# Lens Pizza Slice

Hi Everyone,

I am working on the len’s pizza slice project and am stuck on part 6

" Convert our `toppings` and `prices` lists into a two-dimensional list called `pizza_and_prices` that has the following associated values. “Each sublist in `pizza_and_prices` should have one pizza topping and an associated price”

Is there a way to make a 2d list with two seperate 1d lists? pizza_and_prices = [prices, toppings] is not properly indexed.
Should I manually create this 2d list?

1 Like

Hi @mohejazi,

If you wanted to combine two lists like that, you could use the zip method:

Then cast the resulting tuple to a list

-Lyall

1 Like

That’s what big brother stack overflow had posted as well. This section of the course hasn’t talked about zip or tuples yet, so I’m goonna assume they wanted it manually done. Thank you!

1 Like

Yep, manually. It used to use the `.zip()` method in prior iterations…
until `.zip()` was introduced later on in the course.

1 Like

same question. the phrase “convert the lists in a two-dimensional list” kinda feels like they want you to apply something more hard than just write it by yourself.

if not - okay, but then i wish there were complete codes of the project to look at it at to be sure. since the task is completed just if I check the box myself.

Yea, I think they should update the instructions for that. (b/c it doesn’t match what they’re actually asking one to do. See discussion above or search for other threads on this very topic in the forums).

1 Like

thanks, this thread helped!

On the last code for len’s slice and I am using the sorted method to create a new list for the cheapest pizza.
My code is:

three_cheapeast = sorted([1, “cheese”], [2, “mushrooms”], [2, “olives”])

Getting a error saying: TypeError: an integer is required (got type list)

Is it giving me an error because I did not identify what list I was pulling from?

Looking for explanation - My understanding is that the built-in function of using sorted() would give me exactly what I am looking for since it created a new list all together. I didn’t going with the .sort method because I am not looking for a numerical listing or alphabetical arrangement.

``````three_cheapeast = sorted([1, "cheese"], [2, "mushrooms"], [2, "olives"])
``````

That line should not even work. `sorted()` does not take multiple arguments, but a list or dict object.

1 Like

This might help(?):
https://docs.python.org/3/howto/sorting.html

How did you originally combine the two lists (pizza & prices)?

1 Like

Yeah it didn’t work. It produced the error.

1 Like

Have you sorted it out (pun intended)?

1 Like

This was the combination.

pizza_and_prices = [[2, “pepperoni”], [6, “pineapple”], [1, “cheese”], [3, “sausage”], [2, “olives”], [7, “anchovies”], [2, “mushrooms”]]

And you want the three cheapest from that list?

Have you learned about `lambda`, yet?

How are you with slice? (Pun not intended, but…)

1 Like

No the course hasn’t referenced lambda as of yet. I haven’t sorted it out as of yet.

I meant, what was your code for that? Did you have to manually combine the two lists or did you write a list comprehension or something?

My major issue with this mini-project is that step 6–where one is asked to create a 2-dimensional list—it’s not really very clear. One way to do it isn’t covered until the end of the module on Lists (Hint: `.zip()`) What IS covered up until this point–insert, range, len, pop, sort, sorted, index slicing isn’t helpful at all for this question.
IMO `.zip()` should be covered before this project…

``````pizza_and_prices = [list(a) for a in zip(prices, toppings)]
print(pizza_and_prices)

OR, you could do it this way:

pizza_and_prices = zip(prices, toppings)
print(list(pizza_and_prices))
Which outputs:
[(2, 'pepperoni'), (6, 'pineapple'), (1, 'cheese'), (3, 'sausage'), (2, 'olives'), (7, 'anchovies'), (2, 'mushrooms')]
``````

So, if you’ve created a 2-D list and sorted it, each element of the sublists are easy to access with list slicing.

``````pizza_and_prices.sort()
print(pizza_and_prices)
Which gives you this:
[[1, 'cheese'], [2, 'mushrooms'], [2, 'olives'], [2, 'pepperoni'], [3, 'sausage'], [6, 'pineapple'], [7, 'anchovies']]
``````

Then, how would you access the first element and store it as a variable called `cheapest_pizza`?

How would you access the last, or most expensive element in the list of pizzas?

`lambda` is the root of a function, and acts very much like one with a small exception: implicit return.

``````f = lambda x: x ** 2
``````

`f` is the name we have given our lambda, and `x` is the parameter. The expression on the right of the colon is the return.

``````print (f(6))    #  36
``````

`sorted()` takes an iterable which can be made up of lists or tuples of equal length, plus there are two flags of note to us, one is `reverse`, as in `reverse=False` (the default) and `key`, which is where we use our lambda.

Eg.

``````>>> sorted([[2, "pepperoni"], [6, "pineapple"], [1, "cheese"], [3, "sausage"], [2, "olives"], [7, "anchovies"], [2, "mushrooms"]], key=lambda x: x[0])
``````

Where `x` is the outer list element, and we used the lambda to identify which element in the inner element to sort by. Since the numbers are the first value, then we select element `x[0]`

This gives us,

``````a = [[1, 'cheese'], [2, 'pepperoni'], [2, 'olives'], [2, 'mushrooms'], [3, 'sausage'], [6, 'pineapple'], [7, 'anchovies']]
>>>
``````

The final step would be to take a slice, length 3 from the returned list. This is where we employ the slicing technique we learned in the list unit.

``````three_cheapest_slices = a[:3]
``````

One should note that we didn’t actually need the key since it defaults to index 0 of the inner iterables, so we can write it similar to the way @lisalisaj did in her `.sort()` method. That method is in place so cannot be assigned, hence using `sorted()` which sorts a copy that is then assigned. This method does not alter the original list.

Where the `key` would be useful is if we wanted to sort the list by names, not numbers.

``````>>> sorted([[2, "pepperoni"], [6, "pineapple"], [1, "cheese"], [3, "sausage"], [2, "olives"], [7, "anchovies"], [2, "mushrooms"]], key=lambda x: x[1])
[[7, 'anchovies'], [1, 'cheese'], [2, 'mushrooms'], [2, 'olives'], [2, 'pepperoni'], [6, 'pineapple'], [3, 'sausage']]
>>>
``````

We can use the same technique for sorting `dict.items()` by key or by value. Over time you will see and use this a lot so don’t let it throw you off. It is as simple as it looks, just takes some practice to commit to memory.

1 Like

I just realized that. I ended up creating an entire new list vice converting the two list already available:
#making a 2D list D=dimensional
pizza_and_prices = [[2, “pepperoni”], [6, “pineapple”], [1, “cheese”], [3, “sausage”], [2, “olives”], [7, “anchovies”], [2, “mushrooms”]]

When I accessed the last item in my created list I would have thought to use the [-] or [+] integers in order to access the list value. I am going through the .zip() concept currently trying to wrap my head around it as well as the lambda mtf is referencing.

Yes, to access items from your list you use the index you want. (this is after you’ve sorted the items in accordance to the directions in the lesson.)

ex:

``````cheapest_pizza = pizza_and_prices[0]
print(cheapest_pizza)
[1, 'cheese']

priciest_pizza = pizza_and_prices[-1]
print(priciest_pizza)
[7, 'anchovies']
``````

Correct me if I am wrong. The way I did it is not the way the lesson intended it to be done?