change vars in place w/loop or list comprehension

Raymond Hettinger python at rcn.com
Tue May 31 10:55:52 EDT 2005


> >>> a = 'one'
> >>> b = 'two'
> >>> c = 'three'
> >>> [a, b, c] = [s.upper() for s in [a, b, c]]
> >>> a
> 'ONE'
>
> 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.

seq = a, b, c
a, b, c = seq = [s.upper() for s in seq]




More information about the Python-list mailing list