Behavior of lists in python during assignment

<>

This question already has an answer here:

Answers


try this:

a = gates[:]
b = a[:]

these will make a copy of gates and a lists


All of them are references to the same object, so modifying any one of them will affect all references:

>>> gates=[16, 16, 24, 24, 24, 27, 32, 32, 32, 32, 32, 32, 40, 40, 40, 56, 56, 64, 96];
>>> import sys
>>> sys.getrefcount(gates)  #reference count to the object increased
2
>>> a = gates
>>> sys.getrefcount(gates)  #reference count to the object increased
3
>>> b = a
>>> sys.getrefcount(gates)  #reference count to the object increased 
4

If you want a new copy then assign the new variables to a shallow copy using [:]:

>>> a = [1, 2, 3]
>>> b = a[:]
>>> a[0] = 100
>>> a
[100, 2, 3]
>>> b
[1, 2, 3]

If the list contains mutable objects itself then use copy.deepcopy.


Need Your Help

how to change the tab widget size in TabHost

android android-tabhost

In my application i use the tabhost in linear layout, tabhost contains 4 tabs. The size of the tabwidget is very very high and it shows title very small. Can any one help me how to reduce the size of

Cut a string into an array based on two words in Bash

arrays json bash parsing

I am currently working on a bash shell script and have a string that goes something like this: