how do i get this code to stop referring to its present value, so that the values already saved in the list are not changed to the present value?

Cm = 0x5A827999; Mm = 0x6ED9EBA1
	Cr = 19; Mr = 17
	Tm = [[0]*8]*24
	Tr = [[0]*8]*24
	for i in range(8):
		for j in range(24):
			Tm[j][i] = Cm
			Cm = (Cm + Mm) % 2**32
			Tr[j][i] = Cr
			Cr = (Cr + Mr) % 32

if anyone cares, this is part of CAST-256

8 Years
Discussion Span
Last Post by calccrypto

the lists are supposed to have different values in each index. however, the computer keeps on referring to the last value of the "j" loop and replacing every Tr[x][0] value with Tr[23][0]'s value

say after the first loop, Tr is supposed to be


the code changes everything to


When you use Tm = [[0]*8]*24 you are creating 24 alias copies of an eight zero list. That means all 24 sublists have the same memory address.

To create the proper list of lists use this approach:

# create a 24 by 8 list of lists initialized with zero
zerolist24x8 = []
for x in range(24):
    # create a fresh list of 8 zeros 24 times
    zerolist8 = []
    for y in range(8):

Now you can use module copy to make your copy of zerolist24x8, for instance:

import copy
Tm = copy.deepcopy(zerolist24x8)
Tr = copy.deepcopy(zerolist24x8)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.