• Python question of the day

    What would be the output of executing the following code:
    x = [2, 1, 5, 6, 8, 9, 4]
    [sum(x[i::2]) for i in range(len(x)//(len(x)//2))]
     [19, 16]
     [15, 19, 16]
     [19, 16, 26]
     [19, 16, 21, 25]
    Explanation:
    range(len(x)//(len(x)//2)) will give range(0, 2), meaning the comprehension will be evaluated for [0, 1]. Sum for i = 0 is 2+5+8+4 = 19 and for i = 1 sum is 1+6+9 = 16 (notice that step size is 2)


    Top 100 python interview questions:






datai analytics