EDIT: clarified question based on responses
When I create a list and fill its elements with the content from another list using a for loop and the .format-Function, the values from the original list change as well. I think the behavior is clear when you run the code below (Example 1) and read the printed statements.
However, this behavior cannot be observed in Example 2. Why is that?
Python Version: 3.6.5
Thanks a lot!
Example 1:
list = [i for i in range(5)]
print("The variable 'list' is ", end='')
print(*list)
print()
new_list = list
print("A new variable 'new_list' is created and gets assigned the values from 'list' and is ", end='')
print(*new_list, end='')
print(" as well")
print("The variable 'list' is still ", end='')
print(*list)
print()
print("Each element in 'new_list' gets modified by using a for loop and the '.format-Function")
for column in range(5):
new_list[column] = "X{}".format(list[column])
print("The variable 'new_list' is now ", end='')
print(*new_list)
print()
print("Why did this change the variable 'list'? It is now ", end='')
print(*list, end='')
print(" as well?!")
Example 2:
A = ["A0", "A1"]
print ("A = {}".format(A))
B = ["B0", "B1"]
print ("B = {}".format(B))
B[0] = "X{}".format(A[0])
B[1] = "X{}".format(A[1])
print ("A = {}".format(A))
print ("B = {}".format(B))