1

When I use list_1=[[0]*5]*10to make a list.One element of python list changes, causes the whole column elements changed. But when I use list_1=[[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]to make a list.There is no mistake.Why?

My code:

import random
import csv
rows = 10
cols = 5
list_1 = [[0]*cols]*rows #This is wrong.
#list_1 = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]] #This is right.
for i in range(rows)
    a=random.randint(1,100)
    b=random.randint(1,100)
    c=random.randint(1,100)
    d=random.randint(1,100)
    e=random.randint(1,100)
    list_1[i][0] = a
    list_1[i][1] = b
    list_1[i][2] = c
    list_1[i][3] = d
    list_1[i][4] = e
print(list_1)

When I use list_1 = [[0]*cols]*rows,the result is :

[[12,25,0,1,0],
[12,25,0,1,0],
[12,25,0,1,0],
[12,25,0,1,0],
[12,25,0,1,0],
[12,25,0,1,0],
[12,25,0,1,0],
[12,25,0,1,0],
[12,25,0,1,0],
[12,25,0,1,0]]

All the rows are same.But when I use list_1=[[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]] The result is right.

2 Answers2

3

Consider this simple example to understand:

li = [[]] * 3
print(li)
# Out: [[], [], []]

At first glance we would think we have a list of containing 3 different nested lists. Let's try to append 1 to the first one:

li[0].append(1)
print(li)
# Out: [[1], [1], [1]]

The reason is that [[]] * 3 doesn't create a list of 3 different lists. Rather, it creates a list holding 3 references to the same list object. As such, when we append to li[0] the change is visible in all sub-elements of li. This is equivalent of:

li = []
element = [[]]
li = element + element + element
print(li)
# Out: [[], [], []]
element.append(1)
print(li)
# Out: [[1], [1], [1]]

Reference: https://riptutorial.com/python/example/12259/list-multiplication-and-common-references

Sapan Zaveri
  • 490
  • 7
  • 18
1

The answer by Sapan Zaveri gives a very good explanation as to why this happens.

One solution to this:

import csv
rows = 10
cols = 5
list_1 = []
for i in range(rows):
    a = []
    for j in range(cols):
         a.append(random.randint(1,100))
    list_1.append(a)
print((list_1))

This relieves you from having to explicitly assign values to each row. Hope it helps.

a_r
  • 488
  • 6
  • 12