1

This is something I wish to ask on top of the question that is already discussed:

Assigning values to variables in a list using a loop

So the link above says it is not recommended to perform assignments in a for loop because doing so changes only the reference of those values and doesn't change the original variable.

For example, if I wish to add 1 to each variable:

p = 1
q = 2
r = 3

a = [p,q,r]

for i in a:
  i += 1 

print(p, q, r)

# returns 1 2 3

So this doesn't change p, q, and r, since the values added by one are assigned to 'i'. I inquired some people about a possible solution to this issue, and one suggested that I try the 'enumerate' function. So I did:

p = 1
q = 2
r = 3

a = [p,q,r]

for idx, val in enumerate(a):
  a[idx] = val + 1 


print(p, q, r)

# returns 1 2 3 

So it still doesn't work. Printing the list 'a' does return the list of values added by 1, but it still doesn't change the values assigned to p, q, and r, which is what I want.

So the only solution I currently have is to do the assignment for each variable manually:

p = 1
q = 2
r = 3

p += 1
q += 1
r += 1 

print(p, q, r)

# returns 2 3 4

However, in a hypothetical setting where there are more variables involved than 3, such as 50 variables where I wish to add 1 to each value assigned, doing it manually is going to be very demanding.

So my question is, is there a solution to this without doing it manually? How do we accomplish this using the for loop? Or, if the for loop doesn't work for this case, is there another way?

Robin311
  • 203
  • 1
  • 9

1 Answers1

2

You can maintain a dictionary, where the keys are strings (the names variables that you originally had) and the values are the integers that they're assigned to.

Then, you can do the following:

data = {
    "p": 1,
    "q": 2,
    "r": 3
}

for item in data:
    data[item] += 1

print(data)

This outputs:

{'p': 2, 'q': 3, 'r': 4}
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
  • Wait, does it work if you use dictionaries instead of lists? Could you further elaborate why? – Robin311 Mar 24 '22 at 05:22
  • 1
    @Robin311 no, a list would work the same. The point is, you need *some kind of container*, not 50 variables. In this case, a dict allows you to retrieve a another value with a string, instead of by index. – juanpa.arrivillaga Mar 24 '22 at 05:25
  • @juanpa.arrivillaga I did use lists : `a = [p,q,r]` But this didn't work. But apparently, using dictionaries works. So I'm interested in why. – Robin311 Mar 24 '22 at 05:28
  • @juanpa.arrivillaga : Can you give an example with a list too? – Devang Sanghani Mar 24 '22 at 05:34
  • The page @juanpa.arrivillaga linked is a good read on this (I was going to link it myself, but couldn't find it.) In particular, the page states that "[n]ames are reassigned independently of other names." In your list example, you have two names referring to the same value, and you reassign one of them. In the dictionary example, we only refer to an integer value by one name (using dictionary key access). The aforementioned page is a very good read; if you still have questions about what I've written after reading that page, please let me know. – BrokenBenchmark Mar 24 '22 at 05:34
  • @Robin311 no, it *won't affect any variables*. In this case, you are just using the dict. – juanpa.arrivillaga Mar 24 '22 at 05:34