Python Dictionary Pass by Value/Reference Last Updated : 10 Jan, 2025 Comments Improve Suggest changes Like Article Like Report In Python, dictionaries are passed by reference, not by value. Since dictionaries are mutable, passing them to a function means the original dictionary can be modified.If we want to avoid changes to the original, we can create a copy before passing it. Understanding this behavior is key to managing data effectively in Python.Pass by Reference in Python DictionariesSince dictionaries are mutable items, when we pass a dictionary to a function, the characteristic works with the original dictionary, no longer a duplicate of it. Any modification made to the dictionary within the characteristic will have an effect on the original dictionary. Python def fun(a): a["newkey"] = "new_val" # update the dictionary d = {"key": "old_val"} fun(d) # passing dictionery as a reference print(d) Output{'key': 'old_val', 'newkey': 'new_val'} Explanation :This function fun modifies the dictionary passed to it by adding a new key-value pair .Since dictionaries are passed by reference, the changes are reflected in the original dictionary and the updated dictionary is printed.Pass by Value in Python DictionariesSince dictionaries are mutable, passing one to a function modifies the original. To avoid this, create a copy using .copy() or dict(), so the original remains unchanged. Python def fun(a): a["newkey"] = "new_val" # Modify the dictionary d1 = {"key": "old_val"} d2 = d1.copy() # Create a copy of the dictionary fun(d2) print(d1) print(d2) Output{'key': 'old_val'} {'key': 'old_val', 'newkey': 'new_val'} Explanation:This function fun modifies the dictionary passed to it by adding "newkey": "new_val", but the original d remains unchanged.A copy of d1 is created using .copy(), and the modification occurs on the copy, leaving d unchanged and d2 updated. Comment More infoAdvertise with us Next Article Python Dictionary Pass by Value/Reference V vishakshx339 Follow Improve Article Tags : Python python Practice Tags : pythonpython Similar Reads Pass by reference vs value in Python Developers jumping into Python programming from other languages like C++ and Java are often confused by the process of passing arguments in Python. The object-centric data model and its treatment of assignment are the causes of the confusion at the fundamental level.In the article, we will be discus 7 min read Is Python call by reference or call by value Python utilizes a system, which is known as "Call by Object Reference" or "Call by assignment". If you pass arguments like whole numbers, strings, or tuples to a function, the passing is like a call-by-value because you can not change the value of the immutable objects being passed to the function. 5 min read Python dictionary values() values() method in Python is used to obtain a view object that contains all the values in a dictionary. This view object is dynamic, meaning it updates automatically if the dictionary is modified. If we use the type() method on the return value, we get "dict_values object". It must be cast to obtain 2 min read Use return value in another function - python In Python, one functionâs return value can be used in another, making code cleaner and more modular. This approach simplifies tasks, improves code reuse, and enhances readability. By breaking down logic into smaller functions that share data, you create flexible and maintainable programs. Letâs expl 2 min read Python | Ways to Copy Dictionary Dictionary is a collection which is unordered, changeable and indexed. In Python, dictionaries are written with curly brackets, and they have keys and values. It is widely used in day to day programming, web development, and machine learning. When we simply assign dict1 = dict2 it refers to the same 3 min read Python Dictionary copy() Python Dictionary copy() method returns a shallow copy of the dictionary. let's see the Python Dictionary copy() method with examples: Â Examples Input: original = {1:'geeks', 2:'for'} new = original.copy() // Operation Output: original: {1: 'one', 2: 'two'} new: {1: 'one', 2: 'two'}Syntax of copy() 3 min read How to Replace Values in a List in Python? Replacing values in a list in Python can be done by accessing specific indexes and using loops. In this article, we are going to see how to replace the value in a List using Python. We can replace values in the list in several ways. The simplest way to replace values in a list in Python is by using 2 min read Returning Multiple Values in Python In Python, we can return multiple values from a function. Following are different ways 1) Using Object: This is similar to C/C++ and Java, we can create a class (in C, struct) to hold multiple values and return an object of the class. Python # A Python program to return multiple # values from a meth 4 min read Python - Access Dictionary items A dictionary in Python is a useful way to store data in pairs, where each key is connected to a value. To access an item in the dictionary, refer to its key name inside square brackets.Example:Pythona = {"Geeks": 3, "for": 2, "geeks": 1} #Access the value assosiated with "geeks" x = a["geeks"] print 3 min read Get Variable Name As String In Python In Python, getting the name of a variable as a string is not as straightforward as it may seem, as Python itself does not provide a built-in function for this purpose. However, several clever techniques and workarounds can be used to achieve this. In this article, we will explore some simple methods 3 min read Like