Open In App

Variable Operations Dictionary Update – Python

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

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 and y under the key ‘best’. The resulting dictionary would look like this: {‘Gfg’: 13, ‘best’: 42}.

Using dictionary

In this method, we directly initialize a dictionary with the required operations as values. This is the most efficient way to perform variable operations and update a dictionary. We simply assign the values to the keys based on the operations we want to perform.

Python
# initializing variables
x = 6
y = 7

res = {'Gfg': x + y, 'best': x * y}

print(str(res))

Output
{'Gfg': 13, 'best': 42}

Explanation: This code initializes variables x and y with values 6 and 7, creates a dictionary res with keys ‘Gfg’ and ‘best’ holding the sum and product of x and y, respectively.

Using dictionary comprehension

Dictionary comprehension is a efficient way to construct dictionaries by performing operations on values. We can use conditional logic and directly compute the values in a single line. It is a more dynamic approach compared to direct initialization and is still efficient.

Python
# initializing variables
x = 6
y = 7

res = {key: x + y if key == 'Gfg' else x * y for key in ['Gfg', 'best']}

print(str(res))

Output
{'Gfg': 13, 'best': 42}

Explanation: This code initializes variables x and y, creates a dictionary res where ‘Gfg’ holds the sum of x and y, and ‘best’ holds their product.

Using for loop

This method involves using a for loop to iterate over keys and perform operations on the dictionary values. While it is less concise compared to dictionary comprehension, it is a simple approach and allows more flexibility, such as adding complex conditions or additional logic for updating dictionary values.

Python
# initializing variables
x = 6
y = 7

keys = ['Gfg', 'best']

res = {} # Creates an empty dictionary 
for key in keys:
    if key == 'Gfg':
        res[key] = x + y
    else:
        res[key] = x * y

print(str(res))

Output
{'Gfg': 13, 'best': 42}

Explanation: This code initializes x = 6 and y = 7, iterates over the keys [‘Gfg’, ‘best’], and updates the dictionary res with the sum for ‘Gfg’ and the product for ‘best’.

Using lamda

Lambda functions are used within the dictionary to perform operations dynamically. While this is more compact than defining full functions, it introduces overhead due to the lambda calls.

Python
# initializing variables
x = 6
y = 7

fun = {'Gfg': lambda: x + y, 'best': lambda: x * y}
res = {key: val() for key, val in fun.items()}

print(str(res))

Output
{'Gfg': 13, 'best': 42}

Explanation: This code initializes x = 6 and y = 7, defines a dictionary fun with lambda functions for sum and product, creates res by calling the lambdas.



Next Article
Practice Tags :

Similar Reads