Len's slice task 6 `

I don’t know if the link below will take you to the problem I’m having an issue with but if it does not I
have a screenshot


What I would like to know is, is there a way to get the output of what I circled in the picture?
I added the list using the “+” operation and that didn’t work. Based off what was taught in python 3 so far there was not a way to get the result I circled in the picture unless I manually make the list myself like this
pizza_and_prices = [2, ‘pepperoni’]

[prices + toppings] = failed
[prices, toppings] = failed

You will learn about it in later lessons, but there are two ways to do what you want; both involve using the zip() function.
The first method will produce a list of tuples when using zip() by itself. If you want a list of lists, then you can combine zip() with list comprehension

toppings = ["pepperoni", "pineapple", "cheese", "sausage", "olives", "anchovies", "mushrooms"] prices = [2, 6, 1, 3, 2, 7, 2] # Method 1 using standalone zip pizza_and_prices_using_zip = list(zip(prices, toppings)) print("Using standalone zip method:") print(pizza_and_prices_using_zip) print() # Method 2 using zip with list comprehension pizza_and_prices_with_list_comprehension = [list(element) for element in zip(prices, toppings)] print("Using zip with list comprehension:") print(pizza_and_prices_with_list_comprehension)
1 Like

maybe a list comprehension is what you’re looking for.

prices_and_pizzas = [ [prices[i], pizzas[i]] for i in range(len(pizzas)) ]

I guess you could do that with a loop instead.

prices_of_pizzas = []
length = len(pizzas)
for i in range(length):
  prices_of_pizzas.append( [prices[i], pizzas[i]] )

print(prices_of_pizzas[0])  #// print first list in prices_of_pizzas list of lists
1 Like

Yes, you are correct. I had to take a break for this one, but it shouldn’t be asked if it hasn’t been shown. It could be something don’t know so I’ll go back and see if I can report that section and I guess we’ll see if they’ll make any changes.
Thank you!

This project used to use .zip() but it was then revised & .zip() was removed. The copyediting could have been handled better in the project. I don’t know if list comprehensions are covered prior to this either…