Purpose of zip and list in creating dataset Chapter 4 MNIST

Hi there, how are you doing?

I’m going through chapter 4 at the moment. I’m a bit confused on the used of list and zip in creating dataset.

dset = list(zip(train_x,train_y))
x,y = dset[0]
x.shape,y

I’m not sure what does zip and list do here and especially why x,y is set to be dset[0].

I hope you could help me crack this confusion.

Thank you

Hi @toannguyen,

The zip function aggregates the iterables you pass to it: the first item of train_x and the first item of train_y are paired together in a tuple, and so one.
It gives a structure like [(train_x[0], train_y[0]), (train_x[1], train_y[1]), ...].
The list function then transforms this zip object (which is an iterator) into a list!

So we can then get the first item of this list, which will be the first pair, where x is the first item of train_x and y the first item of train_y.
I don’t have all the context here, but I guess it’s just to check their dimensions & values :slightly_smiling_face:

1 Like

Hi @dway8 ,

Thank you so much for your detailed answer. I really couldnt have asked for a better one

1 Like