Append (key: value) Pair to Dictionary29 Aug 2024 | 4 min read Dictionary is one of the most used data types in Python. It is an unordered collection of key: value pairs. Every value has a corresponding key that identifies it. A dictionary is a mutable collection, meaning we can modify the values. One factor that makes a dictionary unique among other data types is that it stores the mapping of key: value pairs while other data types store a single value as an element. Example:Code Output: {101: 'Ramya', 102: 'Sanya', 103: 'Sree'} Ramya Sanya Sree Given that a dictionary is mutable, we should be able to alter the values of existing keys and add new key: value pairs to the dictionary. This tutorial discusses how we can add a new key: value pair, into a dictionary. 1. Traditional way of using the key Subscripts:We can assign the existing keys with the values we want. Python forgets the old value and updates the new value to the key. Using the same way, we can assign values to new keys, thus appending new pairs. The subscript notation syntax is as follows: Syntax: Example: Code Output: {101: 'Ramya', 102: 'Sanya', 103: 'Sree'} {101: 'Ramya', 102: 'Priyanka', 103 : 'Ujjwala', 104: 'Sanya', 105: 'Sree'} 2. update() Method:It is an inbuilt dictionary method designed to modify a dictionary. The method takes the {key: value} pair as an argument and adds it to the dictionary. We can also update the values of pre-existing keys using this method. The method can accept any number of arguments, which means it can append a dictionary with any number of {key: value} pairs at once. The value would be replaced with the new value even if the key is already present in the dictionary. A new key-value pair will be appended to the dictionary if the key doesn't already exist. Example: Code Output: {101: 'Ramya', 102: 'Sanya', 103: 'Sree'} {101: 'Ramya', 102: 'Priyanka', 103: 'Ujjwala', 104: ' Sanya', 105: 'Sree'} In the above example, the dictionary is modified 4 times using the update method. The first two statements updated the values of already existing keys, while the next two added two new pairs to the dictionary. What if we want to add thousands of new key: value pairs? We can't keep adding individual pairs one after the other. It takes a lot of time, making the code complex and lengthy. The simple way is to create a new dictionary with all the new pairs we want to add and then append/ merge it into the dictionary. Example: Code Output: The original dictionary: {101: 'Ramya', 102: 'Sanya', 103: 'Sree'} New key: value pairs: {104: 'Jeevani', 105: 'Rishitha', 106: 'Nikitha'} The updated dictionary: {101: 'Ramya', 102: 'Sanya', 103: 'Sree', 104: 'Jeevani', 105: 'Rishitha', 106: 'Nikitha'} 3. OOPS Way:We use the OOPS nature of Python to create a function capable of adding new key: value pairs into the dictionary. The logic is the same traditional way, but the difference is we create an object for the dictionary, and the user can input the key: value pairs to add or modify the dictionary. Example: Code Output: #updating existing key: {101: 'Ramya', 102: 'Sanya', 103: 'Sree'} Enter the key you want to modify: 102 Enter the value to update: Priyanka {101: 'Ramya', 102: 'Priyanka', 103: 'Sree'} #adding new key:value pair: {101: 'Ramya', 102: 'Sanya', 103: 'Sree'} Enter the key you want to modify: 104 Enter the value to update: Sanya {101: 'Ramya', 102: 'Sanya', 103: 'Sree', 104: 'Sanya'} 4. Dictionary Comprehension:A single line of code in Python may be used to construct a dictionary, and the dictionary comprehension technique can be used to add a new key-value pair to an existing dictionary. Here is an illustration of how to add a key-value combination using Python's dictionary comprehension feature: Example: Code Output: {'name': 'John', 'age': 30, 'address': '123 Main St.'} |
PyQt5 is a powerful Python library that offers a wide range of widgets for constructing graphical person interfaces. One of the important widgets for inputting numeric values is the QDoubleSpinBox. It is a superior model of the QSpinBox widget that allows customers to enter decimal numbers...
4 min read
In this article, K-means clustering in 1D?will be the main topic. To introduce the technique and illustrate the idea, a basic implementation in 1D?will be used. The notion will be expanded to N dimensions in the following post. This article will concentrate not just on the...
14 min read
What is Sklearn? An open-source Python package to implement machine learning models in Python is called Scikit-learn. This library supports modern algorithms like KNN, random forest, XGBoost, and SVC. It is constructed over NumPy. Both well-known software companies and the Kaggle competition frequently employ Scikit-learn. It aids...
13 min read
We will understand how to solve the problem of getting a binary tree's zig-zag level order traversal in this tutorial. Example: We have a binary tree like Tree: 1 ...
16 min read
Python allows file manipulation (create, save, read, write, delete files, and many more). Python simplifies saving numerous file formats, and saves several file formats. JSON is JavaScript Object Notation. Data is stored and sent via a text-based computer language script (executable) file. Python's json module supports JSON. JSON...
3 min read
In this tutorial, we will learn how we can create a vector using Numpy library. We will also explore basic operation of vector such as performing addition of two vectors, subtraction of two vectors, division of two vectors, multiplication of two vectors, vector dot product and...
5 min read
In this tutorial, we will learn about data-oriented programming in Python (DOP) as an alternative to good old object-oriented programming (OOP). As we can understand by its name, we practice a programming approach where data is put on first and foremost. We can achieve this by following...
12 min read
How to Print Pattern in Python In Python, for loop is used to print the various patterns. Printing the various patterns are most common asked programming questions in the interview. The multiple for loops are used to print the patterns where the first outer loop is used...
25 min read
Bokeh is a Python library which is used for data visualization. It creates its plots using HTML and JavaScript languages, and it also targets modern website browsers to provide presentation elegant, concise construction of novel graphics good high-performance interactivity. In this tutorial, we will learn how to...
2 min read
In this article, we will automate the operation of sending Instagram Messages using Python. First of all, let us look at what is Instagram. Instagram is a prominent social media platform that focuses on the photo and video sharing. It's been operating since 2010 and has maintained...
19 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India