How to convert a list to a 2d list/ Len's slice

Hi guys, how you are all doing? all safe?

i have a question about a problem that i cant seem te resolve by myself.

Right now im in the chapter of Len’s slice, and i seem to have a problem.

the question is that i have to convert a list, to a 2dimensional list.

i dont know what to do.
i have to combine the list in a right way, but i suck at this.

the new variabel is called : pizza_and_prices.
and i have to cobine,the variabel prices, and topping.
so i thaught to do it in this way:

pizza_and_prices = [prices + toppings]

print(pizza_and_prices)

can you guys please tell me what im doing wrong? i really want to understand.

thankyou for your time my friends.

greets Danny from Holland

Are we given two list, prices and toppings? That being the case we would need to create a new list, and make each element either a list or a tuple. Your approach above is combining the two lists into a 1d list (concatenation).

``````y = []
for x in toppings:
i = toppings.index(x)
y.append([x, prices[i]])
``````

or for simplicity, we can use the iterator, `zip()`.

``````    y = zip(toppings, prices)
``````

The returned list will be of tuple pairs.

``````>>> help(zip)
Help on class zip in module builtins:

class zip(object)
|  zip(*iterables) --> zip object
|
|  Return a zip object whose .__next__() method returns a tuple where
|  the i-th element comes from the i-th iterable argument.  The .__next__()
|  method continues until the shortest iterable in the argument sequence
|  is exhausted and then it raises StopIteration.
|
|  Methods defined here:
|
|  __getattribute__(self, name, /)
|      Return getattr(self, name).
|
|  __iter__(self, /)
|      Implement iter(self).
|
|  __next__(self, /)
|      Implement next(self).
|
|  __reduce__(...)
|      Return state information for pickling.
|
|  ----------------------------------------------------------------------
|  Static methods defined here:
|
|  __new__(*args, **kwargs) from builtins.type
|      Create and return a new object.  See help(type) for accurate signature.

>>>
``````

Note that this returns a `zip object`. We can iterate it, but to print it we need to cast it to a list.

``print (list(zip(a, b))``
3 Likes

There’s a dev comment on what’s expected here- [Question]Learn Python 3 - Lists - Len's Slice - #8 by dpatlutcc

I like @mtf’s version but I think they were trying to avoid introducing tuples too soon in the course in case the task seemed a little odd to you (it’s a fairly recent change that used to use zip).

3 Likes

They want me to make a New variabel, and create a new list

I hope one day i can understand this stuff to code from my mind like you guys.

Thankyou.

Greets Danny

1 Like

I think you’re on the right track anyway, the fact that you’re looking/expecting a programming solution for something like this is good. As soon as loops are introduced you’ll likely start spotting multiple routes to simplify this task for yourself.

1 Like

thanks mate.

sometimes i feel a little dissapointed, because i try to lear a chapter, and after a couple of days i dont remember it.
i can recall curtain things, but not like in the moment that i completed the chapter.

my age doesnt help either, im 37, and always had been a carsprayer.

i now are making python files for myself in a map where i explain evrything per chapter.
if i dont know the answer, i can always open my files to look where it went wrong.

thankyou for your message man, sometimes i feel dumb.

have a great evening.

greets Danny

1 Like