Re: change vars in place w/loop or list comprehension

ken.boss@xxxxxxxxxxxxxxx wrote:
I am a python newbie, and am grappling with a fundamental concept.  I
want to
modify a bunch of variables in place.  Consider the following:

a = 'one'
b = 'two'
c = 'three'
[a, b, c] = [s.upper() for s in [a, b, c]]

Both of these accomplish what I'm after; I prefer the second for its brevity. But either approach requires that I spell out my list of vars-to-alter [a, b, c] twice. This strikes me as awkward, and would be difficult with longer lists. Any suggestions for a better way to do this?

While the real question is why you think you need to modify a bunch of variables in place**, here's one solution to the immediate problem:

for name in 'a b c'.split():
    globals()[name] = globals()[name].upper()

That, of course, will work only if they really are globals, and not if they are local variables inside a function. If that's the case, you really want to rethink this anyway (or use Raymond's approach, though it still appears to require spelling out your list of names twice).

** Why you might not want to do this at all:

It's very rare in real code to need to work with a large number of variables as a set like that. More generally you should probably be storing the strings in something like a list and manipulating them there. This would look like this:

shtuff = ['a', 'b', 'c']
for i,item in enumerate(shtuff):
    shtuff[i] = item.upper()

Or, even more likely:

newList = []
for item in shtuff:

shtuff = newList

If you think you really need to do this anyway, consider posting an example of real code from the program you are trying to write, explaining the actual use case, and we can analyze the real situation instead of debating contrived examples.


Relevant Pages

  • RE: epoll design problems with common fork/exec patterns
    ... that two threads may add the same fd twice. ... Only closing a file will. ... It is customary to trim individuals off of CC lists when replying to a ... LKML and not trimming CC's on such posts. ...
  • Ending outline numbered lists when linked to paragraph style
    ... Does anyone know a way to control what style is set when the user hits enter ... '2) Define Font and Paragraph settings ... numbered lists in Word, when you hit enter twice, the list ...
  • Re: merge list of tuples with list
    ... global b twice, the method 'pop' twice, call the method twice, resize the ... and stuff those two new lists into a new list... ... 10000000 loops, best of 3: 0.0325 usec per loop ...
  • Re: Arrangement problems.
    ... do you mean twice as many rows or twice as many columns? ... To see 4 headers above 3 columns can be a little confusing ... replicates Z, Y, X, and M split across two seperate out of order ... lists next to each other. ...
  • Re: Double messages
    ... No other lists have this ... So it must be a problem at Fedora or a more complex problem. ... We're not getting any of your messages twice. ... What does PEBKAC mean? ...