+ 11

How it works

*x, y = [1,2],[3,4],[5,6] print(list(zip(*x+[y] ))) >>>[1,3,5],[2,4,6] I don’t understand what happens when * x and y are added. What arguments does the zip function get? After all, this function should receive two arguments.

14th Aug 2019, 10:52 AM
Mikhail Gorchanyuk
Mikhail Gorchanyuk - avatar
4 Respostas
+ 5
The result is wrong. It looks like this: [(1, 3, 5), (2, 4, 6)] # a list of two tuples It is a good start if you know what x and y are. Although you are actually not quite right. x is a list of lists which is important for the next step: x = [[1,2],[3,4]] In the next step we have the addition of two lists of lists (x + [y]). In such a case the two lists merge. The elements of both lists are put together into a new list leaving us with this: [[1,2],[3,4],[5,6]] Using the star-operator * on that list unpacks it into three single lists which are then directly passed into the zip-function giving you the shown result (which btw happens to be the transponded version of the input if you look at it like a 2d-matrix). But don't worry. In real life people don't do stuff like that. This was just done to make the challenges more difficult.
15th Aug 2019, 4:53 PM
Thoq!
Thoq! - avatar
+ 5
* unpacks a tuple. zip([1, 2], [3, 4], [5, 6]) combines all three lists to new elements, first taking spots[0], then spots[1]...
14th Aug 2019, 3:28 PM
HonFu
HonFu - avatar
+ 4
It did not help. I understand perfectly what x and y are x =[1, 2],[3,4] y = [5,6]
14th Aug 2019, 11:31 AM
Mikhail Gorchanyuk
Mikhail Gorchanyuk - avatar