0
Please explain code
Why the output of this code is [8,0,4] ? arr=[(5,8,0), (2,4)] # is this an array list or a list of tuples? sum = [] # I don't like when people give function builtin names to object ... for x, *y in arr: # please explain the syntax '*y' what does the * before y mean? sum=sum+y print(sum)
1 Resposta
+ 2
With this code, what happens is that the loop will cycle through both tuples within the array, setting the first value of the tuple to x and setting any other value following it to y (hence the weird syntax when declaring it). Once y gets its value (first being [8,0] and then being [4]), it gets pushed to the end of the of the sum array, resulting in the [8,0] and [4] being combined into one single array.