I some lines of code in order to prep the data to be sorted. In order to do this, I needed each item in the list to be a list. For example, if my list was [a, b, c, d]
, the list should be become [[a], [b], [c], [d]]
where a, b, c, and d are integers. My code however returns [[d], [d], [d], [d]]
:
len_of_list = random.randint(8500, 10000)
val_list = range(len_of_list)
list_one = [[-1]]
for index in range(0, len_of_list):
list_one[0][0] = val_list[index]
val_list[index] = list_one[0]
print val_list
The weirdest thing that happens to the code is that when the second-to-last line is replaced with val_list[index] = list_one[0][0]
, it returns the correct values just without the []
. Thus I assumed that removing the last [0]
would return the values with the []
around them. But what is returned was the last integer in the list originally surrounded by []
for all values. This shouldn't happen because list[0][0]
is reset every iteration with list[0][0] = val_list[index]
.
So why is the list being returned as [[d], [d], [d], [d]]
instead of [[a], [b], [c], [d]]
. Of course, the list has at least 8500 unique integers but d
represents the last value in the list or put simply d = val_list[len(val_list) - 1]
.