I'm working advent of code 2021 and can't figure out why my code isn't working. The problem is, instead of properly appending the dictionary with only the number at the index the entire number gets appended. When printing out the values being processed everything looks fine, there's the index and the value, but when I check the dictionary all indexes have the entire numbers.
Please do not provide a solution without explaining what went wrong, I'm doing this to learn, not to copy-paste!
Thanks!
test_list = [
'00100',
'11110',
'10110',
'10111',
'10101',
'01111',
'00111',
'11100',
'10000',
'11001',
'00010',
'01010']
test_dict = dict.fromkeys(range(len(test_list[0])),[])
for reading in test_list:
print(reading)
for index, bit in enumerate(reading):
print(index, bit)
test_dict[index].append(bit)
Simplified example:
lst = ['four', 'asdf', 'sixy']
dct = dict.fromkeys(range(len(lst[0])),[])
for word in lst:
print(word)
for index, letter in enumerate(word):
print(index, letter)
dct[index].append(letter)
Expected result {0:[f,a,s], 1:[o,s,i], 2:[u,d,x], 3:[r,f,y]}. Current result {0:[f,o,u,r,a,s,d,f,s,i,x,y], 1:[f,o,u,r,a,s,d,f,s,i,x,y], ...}