-1

Assume a, b, c are positive integers. In the following comprehension, how many times will the function randint will be called ?

[[[ randint(1,100) for i in range(a)] for j in range(b)]for k in range (c)]

The output is always abc times and not a+b+c.

I want to understand in what order the function call is happening, can you explain the execution order.

1 Answers1

0

Your code is equivalent to the following nested loop.

arr = []
for k in range(c):
    arr1=[]
    for j in range(b):
        arr2 =[]
        for i in range(a):
            arr2.append(randint(1,100))
        arr1.append(arr2)
    arr.append(arr1)
print(arr)

You can understand the order with this.

  • Except for the fact that the list comprehension from the question creates a nested list and this code does not produce any result. – zvone Dec 12 '22 at 08:03
  • Yeah, I added that to understand the order, I will edit the code so that the output also will be produced. – Rashmi Shehana Dec 12 '22 at 08:35