The following code:
#!/usr/bin/python
def func(listtt):
a.reverse()
a = [1, 2, 3]
b = a
print a, b
func(a)
print a, b
a = [1, 2, 3]
b = a + []
print a, b
func(a)
print a, b
produces the output of:
[1, 2, 3] [1, 2, 3]
[3, 2, 1] [3, 2, 1]
[1, 2, 3] [1, 2, 3]
[3, 2, 1] [1, 2, 3]
I understand that in the first example, a and b both point to the same list. My question is how do I create a new identical list object? If i have a list, is b = a + [ ] the best way to create two separate but identical instances of the list? I want to create two variables that point to two different lists/objects that have the exact same values. How do I do that?
Also, how do I know which operations create a new list and which change the current list? I know append changes the list and addition adds the two lists and creates a new one. I found a list of possible operations in https://docs.python.org/3.4/library/stdtypes.html#mutable-sequence-types, but which operations do which?