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”?
- Why is the order in dictionaries and sets arbitrary?
- Remap values in pandas column with a dict, preserve NaNs
- Understanding dict.copy() – shallow or deep?
- Creating a dictionary from a csv file?
- How to merge multiple dicts with same key or different key?
- Accessing elements of Python dictionary by index
- How do I merge a list of dicts into a single dict?
- append multiple values for one key in a dictionary [duplicate]
- Change the name of a key in dictionary
- Dictionary: Get list of values for list of keys
- I’m getting Key error in python
- How do I write a Python dictionary to a csv file? [duplicate]
- Alternative to dict comprehension prior to Python 2.7
- Finding a key recursively in a dictionary
- Error: ” ‘dict’ object has no attribute ‘iteritems’ “
- In Python, when to use a Dictionary, List or Set?
- How to overcome TypeError: unhashable type: ‘list’
- How to index into a dictionary?
- Extract subset of key-value pairs from dictionary?
- Is there a clever way to pass the key to defaultdict’s default_factory?
- Python dictionary : TypeError: unhashable type: ‘list’
- Using a dictionary to select function to execute
- how to iterate through dictionary in a dictionary in django template?
- How do I merge two dictionaries in a single expression?
- How to convert a nested Python dict to object?
- String to Dictionary in Python [duplicate]
- Check if a given key already exists in a dictionary and increment it
- Sorting a dictionary with lists as values, according to an element from the list
- How can I make a dictionary (dict) from separate lists of keys and values?
- Nested dictionary value from key path
- Calculate difference in keys contained in two Python dictionaries
- Python: Elegantly merge dictionaries with sum() of values [duplicate]
- How to pass dictionary items as function arguments in python? [duplicate]
- Is there anything faster than dict()?
- How can one make a dictionary with duplicate keys in Python?
- How do I merge two dictionaries in a single expression in Python?
- Search a list of dictionaries in Python
- Why can’t I use a list as a dict key in python? Exactly what can and cannot be used, and why?
- How can I avoid “RuntimeError: dictionary changed size during iteration” error?
- Elegant way to remove fields from nested dictionaries
- How to convert a pymongo.cursor.Cursor into a dict?
- Subclassing dict: should dict.__init__() be called?
- How to search if dictionary value contains certain string with Python
- Pandas DataFrame performance
- Determine whether a key is present in a dictionary [duplicate]
- Pretty print JSON dumps
- Sort a list of tuples by second value, reverse=True and then by key, reverse=False
- How do I avoid KeyError when working with dictionaries?