0
Guys, I’m trying to create a list of random values inside other predefined lists. Then in the loop it returns me exactly 10 lines with 4 data with different values. But when it exits the loop and I send a print on all lines saved only the random values of the last loop in all indexes.
numbers = 10
x_tes2 = [[0,0,0,0]]*numbers
for key in range(0,numbers):
x_tes2[key][0]=int(random.choice(year))
x_tes2[key][1]=int(random.choice(km_driven))
x_tes2[key][2]=str(random.choice(fuel))
x_tes2[key][3]=str(random.choice(transmission))
print(x_tes2[key][0:4])
print("\n")
print(x_tes2)
Does anyone know why?
When did you do that
x_tes2 = [[0,0,0,0]]*numbers
made all elements of the arrayx_tes2
reference the same array[0,0,0,0]
.– Augusto Vasques
If you are wondering how to multiply lists in python have an answer I wrote a while ago trying to explain better.
– fernandosavio