Python – Dictionary Tuple Values Update
Last Updated :
27 Jan, 2025
The task of updating tuple values in a dictionary involves modifying each tuple in the dictionary by applying a specific operation to its elements. In this case, the goal is to update each element of the tuple based on a given condition, such as multiplying each element by a constant.
For example, given a dictionary like d = {‘Gfg’: (5, 6), ‘is’: (7, 8), ‘best’: (10, 11)}, the goal could be to multiply each element of the tuple by a constant k. After applying the operation, the updated dictionary might look like {‘Gfg’: (15, 18), ‘is’: (21, 24), ‘best’: (30, 33)}.
Using dictionary comprehension
Dictionary comprehension provides a efficient way to construct a new dictionary by iterating over the existing dictionary’s items. When updating dictionary tuple values, dictionary comprehension allows us to modify each tuple’s elements in a clean and one-liner format.
Python
d = {'Gfg': (5, 6), 'is': (7, 8), 'best': (10, 11)}
k = 3 # Define the constant multiplier
res = {key: tuple(val * k for val in values) for key, values in d.items()}
print(str(res))
Output{'Gfg': (15, 18), 'is': (21, 24), 'best': (30, 33)}
Explanation: dictionary comprehension iterates over each key-value pair in d.items(), multiplies each element of the tuple by k and constructs a new dictionary res with the updated tuples.
Using map()
map() applies a specified function to each item of an iterable in this case, each element of the tuple. While it’s a bit more functional-programming oriented, it is still a very efficient way to modify the tuple values inside a dictionary.
Python
d = {'Gfg': (5, 6), 'is': (7, 8), 'best': (10, 11)}
k = 3
res = {key: tuple(map(lambda x: x * k, val)) for key, val in d.items()}
print(str(res))
Output{'Gfg': (15, 18), 'is': (21, 24), 'best': (30, 33)}
Explanation: dictionary comprehension iterate over d, applying map() with a lambda function to multiply each element of the tuples by k (3), and then converts the result into a tuple. The updated dictionary res is formed with the modified tuples and printed.
Using for loop
For loop is the most basic way to iterate over the dictionary’s items and update the tuple values. While this method is not as concise as dictionary comprehension or map(), it is straightforward and highly readable, making it a great choice for beginners or when clarity is more important. As it allows in-place modification of the dictionary, which is more memory-efficient because it avoids creating an additional dictionary.
Python
d = {'Gfg': (5, 6), 'is': (7, 8), 'best': (10, 11)}
k = 3
for key in d: # Iterate over each key in the dictionary 'd'
d[key] = tuple(val * k for val in d[key])
print(str(d))
Output{'Gfg': (15, 18), 'is': (21, 24), 'best': (30, 33)}
Explanation: for loop iterate through the dictionary d and for each key, it updates the associated tuple by multiplying each element of the tuple by k (3). The modified dictionary is then printed.
Similar Reads
Even Values Update in Dictionary - Python
The task of updating even values in a dictionary in Python involves modifying the values associated with specific keys based on a condition, typically checking whether the values are even. For example, consider a dictionary like d = {'gfg': 6, 'is': 4, 'best': 7}. The goal is to update the values by
3 min read
Python Update Dictionary Value by Key
A Dictionary in Python is an unordered collection of key-value pairs. Each key must be unique, and you can use various data types for both keys and values. Dictionaries are enclosed in curly braces {}, and the key-value pairs are separated by colons. Python dictionaries are mutable, meaning you can
3 min read
Set from Dictionary Values - Python
The task is to extract unique values from a dictionary and convert them into a set. In Python, sets are unordered collections that automatically eliminate duplicates. The goal is to extract all the values from the dictionary and store them in a set. For example, given a dictionary like d = {'A': 4,
3 min read
Updating Value List in Dictionary - Python
We are given a dictionary where the values are lists and our task is to update these lists, this can happen when adding new elements to the lists or modifying existing values in them. For example, if we have a dictionary with a list as a value like this: {'gfg' : [1, 5, 6], 'is' : 2, 'best' : 3} the
4 min read
Python - Summation of tuple dictionary values
Sometimes, while working with data, we can have a problem in which we need to find the summation of tuple elements that are received as values of dictionary. We may have a problem to get index wise summation. Letâs discuss certain ways in which this particular problem can be solved. Method #1: Using
4 min read
Python - Tuple value product in dictionary
Sometimes, while working with data, we can have a problem in which we need to find the product of tuple elements that are received as values of dictionary. We may have a problem to get index wise product. Letâs discuss certain ways in which this particular problem can be solved. Method #1 : Using tu
5 min read
Sort Python Dictionary by Value
Python dictionaries are versatile data structures that allow you to store key-value pairs. While dictionaries maintain the order of insertion. sorting them by values can be useful in various scenarios. In this article, we'll explore five different methods to sort a Python dictionary by its values, a
3 min read
How to Add Values to Dictionary in Python
The task of adding values to a dictionary in Python involves inserting new key-value pairs or modifying existing ones. A dictionary stores data in key-value pairs, where each key must be unique. Adding values allows us to expand or update the dictionary's contents, enabling dynamic manipulation of d
3 min read
Python - Update values of a list of dictionaries
The task of updating the values of a list of dictionaries in Python involves modifying specific keys or values within each dictionary in the list based on given criteria or conditions. This task is commonly encountered when working with structured data that needs transformation or enrichment. For ex
4 min read
Variable Operations Dictionary Update - Python
The task of performing variable operations in a dictionary in Python involves updating the values associated with specific keys based on certain operations. For example, consider a scenario where x = 6 and y = 7. The goal could be to store the sum of x and y under the key 'Gfg' and the product of x
3 min read