Because python passes lists by reference
This means that when you write “b=a” you’re saying that a and b are the same object, and that when you change b you change also a, and viceversa
A way to copy a list by value:
new_list = old_list[:]
If the list contains objects and you want to copy them as well, use generic copy.deepcopy():
import copy new_list = copy.deepcopy(old_list)
Related Posts:
- TypeError: ‘int’ object is not subscriptable
- TypeError: ‘int’ object is not callable
- TypeError: ‘int’ object is not callable
- TypeError: ‘int’ object is not callable
- List changes unexpectedly after assignment. Why is this and how can I prevent it?
- Cannot find module cv2 when using OpenCV
- Cannot find module cv2 when using OpenCV
- Python ‘If not’ syntax [duplicate]
- Converting dictionary to JSON
- IndexError: tuple index out of range —– Python
- sklearn error ValueError: Input contains NaN, infinity or a value too large for dtype(‘float64’)
- What is the naming convention in Python for variable and function names?
- TypeError: cannot perform reduce with flexible type
- How to copy a dictionary and only edit the copy
- Could not find a version that satisfies the requirement tensorflow
- ln (Natural Log) in Python
- How do I check if a variable exists?
- ImportError: numpy.core.multiarray failed to import
- Unable to plot Double Bar, Bar plot using pyplot for ndarray
- Cannot find module cv2 when using OpenCV
- Convert list to tuple in Python
- ImportError: numpy.core.multiarray failed to import
- How to remove \xa0 from string in Python?
- Why are Python’s ‘private’ methods not actually private?
- How to create a new text file using Python
- TypeError: write() argument must be str, not bytes (Python 3 vs Python 2 )
- python socket.error: [Errno 98] Address already in use
- How to import files in python using sys.path.append?
- Check string “None” or “not” in Python 2.7
- Change figure size and figure format in matplotlib
- how to update spyder on anaconda
- how does \r (carriage return) work in Python
- What is Python buffer type for?
- AttributeError(“‘str’ object has no attribute ‘read'”)
- IndexError: index 1 is out of bounds for axis 0 with size 1/ForwardEuler
- Python Save to file
- Add list to set?
- TypeError: unsupported operand type(s) for -: ‘list’ and ‘list’
- ImportError: No module named scipy
- AttributeError: ‘datetime’ module has no attribute ‘strptime’
- Python add item to the tuple
- Checking whether pip is installed?
- Converting binary to decimal integer output
- Decoding UTF-8 strings in Python
- How can I read pdf in python?
- No module named setuptools
- How to have an array of arrays in Python
- Importing variables from another file?
- ValueError: unsupported pickle protocol: 3, python2 pickle can not load the file dumped by python 3 pickle?
- ValueError: unsupported pickle protocol: 3, python2 pickle can not load the file dumped by python 3 pickle?
- AttributeError(“‘str’ object has no attribute ‘read'”)
- How to check if type of a variable is string?
- How to use 2to3 properly for python?
- Why does using from __future__ import print_function breaks Python2-style print?
- How to fix: “UnicodeDecodeError: ‘ascii’ codec can’t decode byte”
- Python – ‘ascii’ codec can’t decode byte
- Python Error – int object has no attribute
- Loading a file with more than one line of JSON into Pandas
- Type error Unhashable type:set
- What is the difference between json.load() and json.loads() functions
- Viewing all defined variables
- Python: count repeated elements in the list
- How to XOR two strings in Python
- Printing variables in Python 3.4
- Why I get ‘list’ object has no attribute ‘items’?
- Python 101: Can’t open file: No such file or directory
- TypeError: argument of type ‘NoneType’ is not iterable
- scrapy run spider from script
- Python NameError, variable ‘not defined’
- python error: TypeError: an integer is required
- Python Requests – No connection adapters
- How to detect key presses?
- Numpy, multiply array with scalar
- TypeError: coercing to Unicode: need string or buffer, list found
- ‘virtualenv’ is not recognized as an internal or external command, operable program or batch file
- ImportError: cannot import name NUMPY_MKL
- Getting the name of a variable as a string
- Python: Pandas pd.read_excel giving ImportError: Install xlrd >= 0.9.0 for Excel support
- TypeError: super() takes at least 1 argument (0 given) error is specific to any python version?
- Split string using a newline delimiter with Python
- No module named urllib3
- Convert string to variable name in python
- AttributeError: ‘tuple’ object has no attribute
- Adding +1 to a variable inside a function
- What is the purpose of the single underscore “_” variable in Python?
- dump() missing 1 required positional argument: ‘fp’ in python json
- How to open html file?
- “Python version 2.7 required, which was not found in the registry” error when attempting to install netCDF4 on Windows 8
- Installing scipy for python 2.7
- Install py2exe for python 2.7 over pip: this package requires Python 3.3 or later
- ImportError: No module named IPython
- Does python have header files like C/C++?
- How can I install a .egg Python package on Windows (attempt using easy_install not working)
- Symbol not found: __PyCodecInfo_GetIncrementalDecoder
- Python 2.7 mixing iteration and read methods would lose data
- What does an ‘r’ represent before a string in python?
- Cannot remove entries from nonexistent file
- Set variable in jinja
- Python list index out of range on return value of split
- Python: OSError: [Errno 2] No such file or directory: ”