RKBock
RKBock

Reputation: 13

python - apply Operation on multiple variables

I know that this is a rather silly question and there are similar ones already answered, but they don't quite fit, so... How can I perform the same operation on multiple variables in an efficient way, while "keeping" the individual variables? example:

a = 3
b = 4
c = 5
a,b,c *= 2  #(obviously this does not work)
print(a,b,c) 

What I want as the output in this scenario is 6, 8, 10. It is rather important that I can still use changed variables.

Thank you very much for your answers!

Upvotes: 1

Views: 3268

Answers (6)

Manuel Fedele
Manuel Fedele

Reputation: 1719

You can use map to apply a function to every element of a list with a lambda function to perform your operation. Then use list unpacking to overwrite values in orginal variables.

a = 1
b = 2
c = 3

a,b,c = list(map(lambda x: x*2, [a,b,c]))

print(a,b,c)

# prints: (2, 4, 6)

map returns a generator, that's why we need to explicit create the list to unpack.

Upvotes: 0

Mykola Zotko
Mykola Zotko

Reputation: 17882

You can use the following generator expression:

a, b, c = 3, 4, 5
a, b, c = (2 * i for i in (a, b, c))
print(a, b, c)
# 6 8 10

Upvotes: 1

Alakazam
Alakazam

Reputation: 475

You can store them in a container, and then use map to apply one function on every element of the container
For example with a list:

def function_to_apply(element):
    return element*2

# Define variables and store them in a container
a,b,c = 1,2,3
container = (a,b,c)

# Apply the function on every element with map
container = tuple(map(function_to_apply, container))
a,b,c = container


This can also be done with lambda functions to avoid defining a new function every time

# Define variables and store them in a container
a,b,c = 1,2,3
container = (a,b,c)

# Apply the function on every element with map
container = tuple(map(lambda x: x*2, container))
a,b,c = container

If you have a really large set of variables and you want to retrieve them automatically without having to type each one of them like in a,b,c = container, you can use dict to store them with names or exec function to assign them dynamically.

map documentation: https://docs.python.org/3/library/functions.html#map
lambda documentation: https://docs.python.org/3/reference/expressions.html#grammar-token-lambda-expr

Upvotes: 0

yardstick17
yardstick17

Reputation: 4592

You can use numpy or python lambda function combined with map to do the same.

Using numpy:

In [17]: import numpy as np

In [18]: a = 3
    ...: b = 4
    ...: c = 5
    ...:

In [19]: a,b,c = np.multiply([a, b, c], 2)

In [20]: a
Out[20]: 6

In [21]: b
Out[21]: 8

In [22]: c
Out[22]: 10

Using lambda:

In [23]: a, b, c = list(map(lambda x: x*2, [a, b, c]))

In [24]: a
Out[24]: 12

In [25]: b
Out[25]: 16

In [26]: c
Out[26]: 20

Upvotes: 1

NMunro
NMunro

Reputation: 910

Map is your friend here, but you also need to use 'implicit tuple unpacking':

>>> a = 3
>>> b = 4
>>> c = 5
>>> d, e, f = map(lambda x: x * 2, [a, b, c])
>>> d
6
>>> e
8
>>> f
10

This way you can get the changed values back without modifying the original values

Upvotes: 0

Seiche
Seiche

Reputation: 3

You may want to look into Python's map function. The following link may be helpful:https://www.w3schools.com/python/ref_func_map.asp

Upvotes: 0

Related Questions