In the Python Dictionary, Can One Key Hold More Than One Value?Introduction:In this tutorial, we learned that one key can hold more than one value or not in the Python dictionary. Dictionaries are Python's representation of data structures and can be thought of as similar to maps in C++. It is a dynamic data structure that dynamically stores key-value pairs, and it is also mutable. It can be better understood by an associative array in which each element is associated with its main value. Although the dictionaries in Python store the values as key-value pairs, multiple values corresponding to the same key can be stored in the dictionary. This is done by setting containers such as lists, sets, and tuples as properties for each key of the dictionary. In this tutorial, we will focus on the steps of creating a dictionary with multiple values of a single key. The steps are given below -
Now we will learn about these steps for creating a dictionary with multiple values of a single key - Step 1: Explicit the declaration of a listWe can explicitly declare a list of multiple elements and assign them to a key. The detailed procedure involves initializing the key with a list containing a single value and then updating the list by using the assignment operator. Syntax: The syntax for explicitly declaring a list of multiple elements is given below - Here, we add values to a list that is initially defined as the value of a dictionary value (starting from the first expression). As shown in the second statement, you can add content to the list using the assignment operator, similar to string concatenation. Algorithm: The algorithm for explicitly declaring a list of multiple elements is given below -
Program Code: Here, we give a program code to declare a list of multiple elements in Python explicitly. The code is given below - Output: Now, we run the above code and find the list of multiple elements. The result is given below - The name list is: {'passed': ['Riya'], 'not passed': ['Priya'], 'excess': ['Hiya']} The name list is: {'passed': ['Riya', 'Neha', 'another Neha', 'and another Neha'], 'not passed': ['Priya'], 'excess': ['Hiya']} Step 2: Adding the values by using the extend() functionThe extend() method is used in the same way as the operator to add a value to a key element. Instead of using '+=' with a key, we add the .extend() function above the key, which adds a new value to the list for that key. Syntax: The syntax for adding a value by using the extend() function is given below - Here, we can see that the extend() method is called on the dictionary for the key value. The extend() function accepts only one argument, so only a single value can be added to the list at a time. If we add a separate list of values, then the interpreter will return a TypeError. Algorithm: The algorithm for adding a value by using the extend() function is given below -
Program Code: Here we give a program code for adding a value by using the extend() function in Python. The code is given below - Output: Now, we run the above code and find the list of multiple elements. The result is given below - The name list is: {'passed': ['Riya'], 'not passed': ['Priya'], 'excess': ['Hiya']} The name list is: {'passed': ['Riya', 'Neha', 'another Neha', 'and another Neha'], 'not passed': ['Priya'], 'excess': ['Hiya']} Step 3: Adding the sublists to a list by using the append() functionUnlike the assignment operator or extend() method used in the past, the append() method behaves slightly differently; instead of adding a value to an existing list in the dictionary, this function creates a sublist in the list and stores the value in that list. Therefore, it can be considered an expression that the size of the list increases after each insertion process. The validity of this method depends on how deep our application needs to be because it also has the disadvantage of rapidly increasing complexity. Syntax: The syntax for adding the sublists to a list by using the append() function is given below - The syntax is similar to the extend() method, so both methods are great when they first work. Call append() on the desired dictionary key, passing in the value of the list. Also here only one argument can be passed to the append() function. If we pass a separate list of sublists, then it will throw a TypeError. Algorithm: The algorithm for adding the sublists to a list by using the append() function is given below -
Program Code: Here we give a program code for adding the sublists to a list by using the append() function in Python. The code is given below - Output: Now, we run the above code and find the list of multiple elements. The result is given below - The name list is: {'passed': ['Riya'], 'not passed': ['Priya'], 'excess': ['Hiya']} The name list is: {'passed': ['Riya', ['Neha']], 'not passed': ['Priya'], 'excess': ['Hiya']} Neha Step 4: Adding the values by using the setdefault() methodHere, we use the setdefault() method to add more values to the key. If a unique key already exists in the dictionary, this function will add the value passed to that key. Otherwise, a new key will be created with a new value by default. We can use methods like extend() to add a new value to the existing value of a particular key. Syntax: The syntax for adding the values by using the setdefault() method is given below - In the above syntax, we pass two arguments to the setdefault() function because we do not know whether the specified key already exists in the dictionary or not. If we are sure the key exists in the dictionary, then we can ignore the second argument (blank list). We then use the extend() function to add a value to the list paired with the key. Algorithm: The algorithm for adding the values by using the setdefault() method is given below -
Program Code: Here we give a program code for adding the values by using the setdefault() method in Python. The code is given below - Output: Now, we run the above code and find the list of multiple elements. The result is given below - The name list is: {'passed': ['Riya'], 'not passed': ['Priya'], 'excess': ['Hiya']} The name list is: {'passed': ['Riya'], 'not passed': ['Priya'], 'excess': ['Hiya'], 'restocked': ['Neha', 'Nisha']} Step 5: Adding the tuples by using the defaultdict() methodThis is the only method we will talk about, and it works on tuples instead of lists. Since tuples are immutable, we cannot pass individual values into tuples linked to keys. Here is the defaultdict() function, which is a subclass of the dict class. We use tuples to assign key-value pairs, and if the key already exists, we add the value to the existing list. Syntax: The syntax for adding the tuples by using the defaultdict() method is given below - Algorithm: The algorithm for adding the tuples by using the defaultdict() method is given below -
Program Code: Here we give a program code for adding the tuples by using the defaultdict() method in Python. The code is given below - Output: Now, we run the above code and find the list of multiple elements. The result is given below - The name list is: {'Name1': ['Riya', 'Neha', 'Nisha'], 'Name2': ['Priya'], 'Name3': ['Hiya']} Conclusion:In this tutorial, we learned whether the Python dictionary allows one key to hold more than one value. Therefore, we conclude that a key in the Python dictionary can have multiple values. We also looked at different ways to add multiple values to a single key in the Python dictionary. There are also many ways to add values to keys, which can be examined in detail later. Here, we learn the syntax, algorithm, and program code of each method. Next TopicInheritance and composition in python |
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