1
l1=list(range(1,31))
l2=list(range(1,31))
l3=list(range(1,31))
lt=[]
for i in l1:
    for j in l2:
        for k in l3:
            if i!=j and j!=k and k!=i:
                m=i+j+k
                if (i+j+k)//3==0:
                    lt.append(m)
                else:
                    pass
print(lt)

Output:

[]

Why above code is printing empty list although values are being appended?

Tanmay Harsh
  • 63
  • 1
  • 7
  • 1
    How do you know values are being appended? – Oliver.R May 19 '20 at 06:03
  • 3
    Think about when the condition `(i+j+k)//3` is zero for positive `i`, `j` and `k`, where they all have to be different values from your first conditional statement. – Unn May 19 '20 at 06:07
  • "Is there any limitation of lists' size in python or some restriction on append method of list in python?" No there isnt' (well, theoretically it is limited by the address space on your system, which is `9223372036854775807` on a 64bit system and `2147483647` on a 32bit system, but that wouldn't give you an empty lists.... and you'd probably run out of memory well before reaching that limit anyway). The probolem is your `if` condition is never true... – juanpa.arrivillaga May 19 '20 at 06:07
  • If you are curious, the max size is only 500M on 32 bit: https://stackoverflow.com/questions/855191/how-big-can-a-python-list-get – Cireo May 19 '20 at 06:17
  • You also probably mean `if m % 3 == 0: lt.append(m)` – Cireo May 19 '20 at 06:18
  • Yes I meant m%3 and not m//3 , thanks all for correcting my mistake and for extra informations... – Tanmay Harsh May 19 '20 at 06:23

2 Answers2

2

The check if (i+j+k)//3==0: effectively checks if the sum of the three numbers divided by three is greater than or equal to zero and less than one. As each of these three numbers need to be unique, and is at least 1, this condition will never be met, and as such your list will stay empty.

Was this meant to be a check of if it is a multiple of 3? if (i+j+k)%3==0:

Oliver.R
  • 1,282
  • 7
  • 17
0

Note that // is floor division and (i+j+k)=0 only if (i+j+k)<3. In fact your condition is never satisfied. For more info check here.

Also note that you do not need to make a list of your ranges, i.e., l1=range(1,31) would be enough.

Ala Tarighati
  • 3,507
  • 5
  • 17
  • 34