3

Say I have a list of variables with the following values

a, b, c = 1, 2, 3

How can I update the value of a,b,c in a for loop?

I've tried the following but it doesn't work.

for v in (a, b, c):
    v = v + 1

The values for a,b,c remains unchanged after the process

print(a, b, c)
# [1] 1 2 3
Harshal Parekh
  • 5,918
  • 4
  • 21
  • 43
Cosmos547
  • 101
  • 6
  • I think you'll have to do it manually. Here a, b, c are called by reference, therefore, the update only exists for the local variable `v` and not `a, b, c` – Cosmos Zhu Sep 01 '20 at 06:38
  • 2
    It's not really clear to me what you'd like to achieve, why the `for loop`? – po.pe Sep 01 '20 at 06:39
  • @CosmosZhu What do you mean by called by reference? I'm not familiar with this and googling just makes me more confused. – Cosmos547 Sep 01 '20 at 06:39
  • @Cosmos547 Simply it means that during the for loop the operations do not apply to the original variable. – Cosmos Zhu Sep 01 '20 at 06:40
  • @CosmosZhu Ok, but I have many variables to update. Doing them one by one will be really undesiring... – Cosmos547 Sep 01 '20 at 06:41
  • 1
    You're literally assigning to the variable `v`, that indeed won't change the variables `a`, `b` nor `c`. You'd have to assign to the variable *by name*, but once you go there, you're usually on the wrong track. There are probably way better approaches to whatever problem you're trying to implement here that render this issue moot. – deceze Sep 01 '20 at 06:41
  • @Cosmos547 Maybe consider putting the variables in a list? List variables are accessed by reference and your for loop will work. – Cosmos Zhu Sep 01 '20 at 06:42
  • @CosmosZhu Thanks, I think others have already given solutions based on lists. – Cosmos547 Sep 01 '20 at 06:43
  • @Cosmos547 No problem. Yeah, that's one way to do it. : ) – Cosmos Zhu Sep 01 '20 at 06:46
  • Related: [If two variables point to the same object, why doesn't reassigning one variable affect the other?](https://stackoverflow.com/questions/56667280/if-two-variables-point-to-the-same-object-why-doesnt-reassigning-one-variable) – MisterMiyagi Sep 01 '20 at 09:34
  • Can you clarify what you are trying to achieve? Surely your use-case is not "increase these variables", but rather the variables and their increase stand for something? – MisterMiyagi Sep 01 '20 at 09:35
  • @MisterMiyagi I need to perform the same transformation to a large set of variables (which are attributes of an object). Using a for loop to go through them doesn't change the variable. Which got me curious to why and how can I do it elegantly. – Cosmos547 Sep 01 '20 at 11:41
  • @Cosmos547 If they are attributes, then you can programmatically set them as ``setattr(obj, name, getattr(obj, name) + 1)``. Of course, details may vary depending on the specific use-case. – MisterMiyagi Sep 01 '20 at 11:43

2 Answers2

5

Try this:

a, b, c = [v+1 for v in (a,b,c)]
dimay
  • 2,768
  • 1
  • 13
  • 22
3

Using lists or dicts

If you have many related variables, a good choice is to store them in a list or dict. This has many advantages over having many separate variables:

  1. Easy to perform an operation on all of them (like updating, printing, saving).
  2. Doesn't litter your code/namespace with an abundance of variables.
  3. Better maintainable code: Less places to change, if you need to add/remove a variable.

Update values in a for loop

# example using a dict

# give them a meaningful name
counters = {"a": 1, "b": 2, "c": 3}

for key in counters:
    counters[key] += 1

Comparing this to updating separate variables in a loop

a,b,c = [v+1 for v in (a,b,c)] This creates an intermediate list with the new values, assigns these to your variables and then throws away the list again. This is an unnecessary intermediate step, but not a big issue.

The real problem with this is, that you still have to write out every variable name twice. It's hardly an improvement over just writing a,b,c = a+1,b+1,c+1.

Wups
  • 2,489
  • 1
  • 6
  • 17