Why using .append() in the for loop generate all same elements in the list?

Good morning:
This is part of the code in the project Thread Shed. https://www.codecademy.com/paths/computer-science/tracks/cspath-python-objects/modules/cspath-python-strings/projects/thread-shed

In (if i % 4 == 3:) part. After I print temp_thread, I append temp_thread to the thread_sold list. Intuitively, temp_thread in this two statements are supposed to be same.

When I print temp_thread in (if i % 4 == 3:) part, it generates the right output, which changes during the iteration. However, when I print thread_sold, which should be a list of all temp_thread, it changes to the same thing. All the elements in the thread_sold list become [‘green&yellow&blue’, ‘09/15/17’].

Can anybody tell me why?

Python code:
transactions_clean =
[‘Edith Mcbride’, ‘$1.21’, ‘white’, ‘09/15/17’, ‘Herbert Tran’, ‘$7.29’, ‘white&blue’,
‘09/15/17’, ‘Paul Clarke’, ‘$12.52’, ‘white&blue’, ‘09/15/17’, ‘Lucille Caldwell’, ‘$5.13’,
‘white’, ‘09/15/17’, ‘Eduardo George’, ‘$20.39’, ‘white&yellow’, ‘09/15/17’, ‘Danny Mclaughlin’,
‘$30.82’, ‘purple’, ‘09/15/17’, ‘Stacy Vargas’, ‘$1.85’, ‘purple&yellow’, ‘09/15/17’, ‘Shaun Brock’,
‘$17.98’, ‘purple&yellow’, ‘09/15/17’, ‘Erick Harper’, ‘$17.41’, ‘blue’, ‘09/15/17’, ‘Michelle Howell’,
‘$28.59’, ‘blue’, ‘09/15/17’, ‘Carroll Boyd’, ‘$14.51’, ‘purple&blue’, ‘09/15/17’, ‘Teresa Carter’, ‘$19.64’,
‘white’, ‘09/15/17’, ‘Jacob Kennedy’, ‘$11.40’, ‘white&red’, ‘09/15/17’, ‘Craig Chambers’, ‘$8.79’, ‘white&blue&red’,
‘09/15/17’, ‘Peggy Bell’, ‘$8.65’, ‘blue’, ‘09/15/17’, ‘Kenneth Cunningham’, ‘$10.53’, ‘green&blue’, ‘09/15/17’,
‘Marvin Morgan’, ‘$16.49’, ‘green&blue&red’, ‘09/15/17’, ‘Marjorie Russell’, ‘$6.55’, ‘green&blue&red’, ‘09/15/17’,
‘Israel Cummings’, ‘$11.86’, ‘black’, ‘09/15/17’, ‘June Doyle’, ‘$22.29’, ‘black&yellow’, ‘09/15/17’, ‘Jaime Buchanan’,
‘$8.35’, ‘white&black&yellow’, ‘09/15/17’, ‘Rhonda Farmer’, ‘$2.91’, ‘white&black&yellow’, ‘09/15/17’, ‘Darren Mckenzie’,
‘$22.94’, ‘green’, ‘09/15/17’, ‘Rufus Malone’, ‘$4.70’, ‘green&yellow’, ‘09/15/17’, ‘Hubert Miles’, ‘$3.59’,
‘green&yellow&blue’, ‘09/15/17’]

thread_sold =
temp_thread = [, ]
thread_sold1 =
for i in range(len(transactions_clean)):
if i % 4 == 2:
temp_thread[0] = transactions_clean[i]
thread_sold1.append(transactions_clean[i])
if i % 4 == 3:
temp_thread[1] = transactions_clean[i]
thread_sold1.append(transactions_clean[i])
print(temp_thread)
thread_sold.append(temp_thread)

print(’\n\n’)
print(thread_sold)
print(’\n\n’)
print(thread_sold1)

Output:
[‘white’, ‘09/15/17’]
[‘white&blue’, ‘09/15/17’]
[‘white&blue’, ‘09/15/17’]
[‘white’, ‘09/15/17’]
[‘white&yellow’, ‘09/15/17’]
[‘purple’, ‘09/15/17’]
[‘purple&yellow’, ‘09/15/17’]
[‘purple&yellow’, ‘09/15/17’]
[‘blue’, ‘09/15/17’]
[‘blue’, ‘09/15/17’]
[‘purple&blue’, ‘09/15/17’]
[‘white’, ‘09/15/17’]
[‘white&red’, ‘09/15/17’]
[‘white&blue&red’, ‘09/15/17’]
[‘blue’, ‘09/15/17’]
[‘green&blue’, ‘09/15/17’]
[‘green&blue&red’, ‘09/15/17’]
[‘green&blue&red’, ‘09/15/17’]
[‘black’, ‘09/15/17’]
[‘black&yellow’, ‘09/15/17’]
[‘white&black&yellow’, ‘09/15/17’]
[‘white&black&yellow’, ‘09/15/17’]
[‘green’, ‘09/15/17’]
[‘green&yellow’, ‘09/15/17’]
[‘green&yellow&blue’, ‘09/15/17’]

[[‘green&yellow&blue’, ‘09/15/17’], [‘green&yellow&blue’, ‘09/15/17’], [‘green&yellow&blue’, ‘09/15/17’], [‘green&yellow&blue’, ‘09/15/17’], [‘green&yellow&blue’, ‘09/15/17’], [‘green&yellow&blue’, ‘09/15/17’], [‘green&yellow&blue’, ‘09/15/17’], [‘green&yellow&blue’, ‘09/15/17’], [‘green&yellow&blue’, ‘09/15/17’], [‘green&yellow&blue’, ‘09/15/17’], [‘green&yellow&blue’, ‘09/15/17’], [‘green&yellow&blue’, ‘09/15/17’], [‘green&yellow&blue’, ‘09/15/17’], [‘green&yellow&blue’, ‘09/15/17’], [‘green&yellow&blue’, ‘09/15/17’], [‘green&yellow&blue’, ‘09/15/17’], [‘green&yellow&blue’, ‘09/15/17’], [‘green&yellow&blue’, ‘09/15/17’], [‘green&yellow&blue’, ‘09/15/17’], [‘green&yellow&blue’, ‘09/15/17’], [‘green&yellow&blue’, ‘09/15/17’], [‘green&yellow&blue’, ‘09/15/17’], [‘green&yellow&blue’, ‘09/15/17’], [‘green&yellow&blue’, ‘09/15/17’], [‘green&yellow&blue’, ‘09/15/17’]]

[‘white’, ‘09/15/17’, ‘white&blue’, ‘09/15/17’, ‘white&blue’, ‘09/15/17’, ‘white’, ‘09/15/17’, ‘white&yellow’, ‘09/15/17’, ‘purple’, ‘09/15/17’, ‘purple&yellow’, ‘09/15/17’, ‘purple&yellow’, ‘09/15/17’, ‘blue’, ‘09/15/17’, ‘blue’, ‘09/15/17’, ‘purple&blue’, ‘09/15/17’, ‘white’, ‘09/15/17’, ‘white&red’, ‘09/15/17’, ‘white&blue&red’, ‘09/15/17’, ‘blue’, ‘09/15/17’, ‘green&blue’, ‘09/15/17’, ‘green&blue&red’, ‘09/15/17’, ‘green&blue&red’, ‘09/15/17’, ‘black’, ‘09/15/17’, ‘black&yellow’, ‘09/15/17’, ‘white&black&yellow’, ‘09/15/17’, ‘white&black&yellow’, ‘09/15/17’, ‘green’, ‘09/15/17’, ‘green&yellow’, ‘09/15/17’, ‘green&yellow&blue’, ‘09/15/17’]

1 Like

If you put a list 3 times into another list, then you’ll … you know, see it 3 times in that list.

a = []
b = [a, a, a]  # [[], [], []]
a.append(1)
b  # [[1], [1], [1]]
1 Like