Open In App

Python – Dictionary Tuple Values Update

Last Updated : 27 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.



Next Article
Practice Tags :

Similar Reads