Python never implicitly copies objects. When you set dict2 = dict1
, you are making them refer to the same exact dict object, so when you mutate it, all references to it keep referring to the object in its current state.
If you want to copy the dict (which is rare), you have to do so explicitly with
dict2 = dict(dict1)
or
dict2 = dict1.copy()
Related Contents:
- Are dictionaries ordered in Python 3.6+?
- Is there a difference between “==” and “is”?
- Understanding dict.copy() – shallow or deep?
- Accessing dict keys like an attribute?
- Split / Explode a column of dictionaries into separate columns with pandas
- How do I print the key-value pairs of a dictionary in python
- Object of custom type as dictionary key
- Convert [key1,val1,key2,val2] to a dict?
- In Python, how do I iterate over a dictionary in sorted key order?
- ‘dict’ object has no attribute ‘has_key’
- Accessing items in an collections.OrderedDict by index
- How to check if one dictionary is a subset of another larger dictionary?
- How to use dot notation for dict in python?
- Destructuring-bind dictionary contents
- Accessing elements of Python dictionary by index
- heapq with custom compare predicate
- How to find the min/max value of a common key in a list of dicts?
- Is there a clever way to pass the key to defaultdict’s default_factory?
- Writing a dictionary to a csv file with one line for every ‘key: value’
- How do I combine two lists into a dictionary in Python? [duplicate]
- Slicing a dictionary
- Find all occurrences of a key in nested dictionaries and lists
- Python functions call by reference [duplicate]
- Make a dictionary with duplicate keys in Python
- What is the purpose of collections.ChainMap?
- How to add an element to the beginning of an OrderedDict?
- Output pyodbc cursor results as python dictionary
- Difference between dict and set (python)
- How do I make a dictionary with multiple keys to one value?
- 5 maximum values in a python dictionary
- Access dict key and return None if doesn’t exist
- convert csv file to list of dictionaries
- Why updating “shallow” copy dictionary doesn’t update “original” dictionary? [duplicate]
- Appending a dictionary to a list in a loop
- Plot a histogram from a Dictionary
- Why can a dictionary be unpacked as a tuple?
- list to dictionary conversion with multiple values per key?
- How to sort a list of dictionaries by a value of the dictionary in Python?
- Create dictionary from list of variables
- Case insensitive dictionary search? [duplicate]
- Python arguments as a dictionary
- Python: Collections.Counter vs defaultdict(int)
- Python dictionary keys besides strings and integers?
- Dict merge in a dict comprehension
- query from postgresql using python as dictionary
- Test if dict contained in dict
- Prettyprint to a file?
- Checking dict keys to ensure a required key always exists, and that the dict has no other key names beyond a defined set of names
- Override the {…} notation so i get an OrderedDict() instead of a dict()?
- Elegant way to remove fields from nested dictionaries