Add new keys to a dictionary in Python Last Updated : 26 Apr, 2025 Comments Improve Suggest changes Like Article Like Report In this article, we will explore various methods to add new keys to a dictionary in Python. Let's explore them with examples:Using Assignment Operator (=)The simplest way to add a new key is by using assignment operator (=). Python d = {"a": 1, "b": 2} d["c"] = 3 print(d) Output{'a': 1, 'b': 2, 'c': 3} Explanation: d["c"]: creates a new key "c" and its value is assigned as "3". If key "c" already exists then it will replace the existing value with new value.Using update()The update() method can be use to merge dictionaries or add multiple keys and their values in one operation. Python d = {"a": 1, "b": 2} # Adding a single key-value pair d.update({"c": 3}) # Adding multiple key-value pairs d.update({"d": 4, "e": 5}) print(d) Output{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5} Explanation:update() accepts another dictionary as an argument and adds its key-value pairs.If a key already exists then its value is updated.Using | Operator (Python 3.9+)We can use | operator to create a new dictionary by merging existing dictionaries or adding new keys and values. This method does not modify the original dictionaries but returns a new dictionary with updated data. Python d = {"a": 1, "b": 2} res = d | {"c" : 3} print(res) Output{'a': 1, 'b': 2, 'c': 3} Explanation:d | {"c": 3} creates a new dictionary by merging d with another dictionary containing key "c".original dictionary d remains unchanged.if keys overlap, values from the right-hand operand overwrite those on the left.Note: If there are duplicate keys then value from the right hand dictionary overwrites the value from the left hand dictionary.Related Articles:Python Dictionary setdefault() MethodHow to add values to dictionary in PythonAppend Dictionary Keys and Values in PythonAdd a key:value pair to dictionary in PythonAdd item after given Key in dictionary Comment More infoAdvertise with us Next Article Add new keys to a dictionary in Python S Shivam_k Follow Improve Article Tags : Python python-dict Python dictionary-programs python Practice Tags : pythonpythonpython-dict Similar Reads Python Remove Dictionary Item Sometimes, we may need to remove a specific item from a dictionary to update its structure. For example, consider the dictionary d = {'x': 100, 'y': 200, 'z': 300}. If we want to remove the item associated with the key 'y', several methods can help achieve this. Letâs explore these methods.Using pop 2 min read Get length of dictionary in Python Python provides multiple methods to get the length, and we can apply these methods to both simple and nested dictionaries. Letâs explore the various methods.Using Len() FunctionTo calculate the length of a dictionary, we can use Python built-in len() method. It method returns the number of keys in d 3 min read Python - Value length dictionary Sometimes, while working with a Python dictionary, we can have problems in which we need to map the value of the dictionary to its length. This kind of application can come in many domains including web development and day-day programming. Let us discuss certain ways in which this task can be perfor 4 min read Python - Dictionary values String Length Summation Sometimes, while working with Python dictionaries we can have problem in which we need to perform the summation of all the string lengths which as present as dictionary values. This can have application in many domains such as web development and day-day programming. Lets discuss certain ways in whi 4 min read Calculating the Product of List Lengths in a Dictionary - Python The task of calculating the product of the lengths of lists in a dictionary involves iterating over the dictionaryâs values, which are lists and determining the length of each list. These lengths are then multiplied together to get a single result. For example, if d = {'A': [1, 2, 3], 'B': [4, 5], ' 3 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 Dictionary items in value range in Python In this article, we will explore different methods to extract dictionary items within a specific value range. The simplest approach involves using a loop.Using LoopThe idea is to iterate through dictionary using loop (for loop) and check each value against the given range and storing matching items 2 min read Ways to change keys in dictionary - Python Given a dictionary, the task is to change the key based on the requirement. Let's see different methods we can do this task in Python. Example:Pythond = {'nikhil': 1, 'manjeet': 10, 'Amit': 15} val = d.pop('Amit') d['Suraj'] = val print(d)Output{'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explanation:T 2 min read Python Program to Swap dictionary item's position Given a Dictionary, the task is to write a python program to swap positions of dictionary items. The code given below takes two indices and swap values at those indices. Input : test_dict = {'Gfg' : 4, 'is' : 1, 'best' : 8, 'for' : 10, 'geeks' : 9}, i, j = 1, 3 Output : {'Gfg': 4, 'for': 10, 'best': 4 min read Merging or Concatenating two Dictionaries in Python Combining two dictionaries is a common task when working with Python, especially when we need to consolidate data from multiple sources or update existing records. For example, we may have one dictionary containing user information and another with additional details and we'd like to merge them into 2 min read Like