mercredi 24 juin 2020

How to get the list append right with if-elif?

The following code ---

lu,area = ([],)*2
row = [10,20]
column = [1,2]

for x in row:
  for y in column:
            if y==1:
                lu.append(y)
                print(lu)
                print(area)
                
            elif y==2:
                area.append(y)
                print(lu)
                print(area)

is printing ---

[1]
[1]
[1, 2]
[1, 2]
[1, 2, 1]
[1, 2, 1]
[1, 2, 1, 2]
[1, 2, 1, 2]

But, the desired outcome is ---

[1]
[]
[1]
[2]
[1, 1]
[2]
[1, 1]
[2, 2]

Why are both lists being appended together inspite of the if-elif logic?

Thanks for taking the time out to help.

Aucun commentaire:

Enregistrer un commentaire