469,625 Members | 1,171 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,625 developers. It's quick & easy.

change vars in place w/loop or list comprehension

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'
list = [a, b, c]
for i in range(len(list)): .... list[i] = list[i].upper()
.... [a, b, c] = list
a 'ONE'

or, better:
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. Any suggestions for a better way to do this?

--Ken

Jul 19 '05 #1
3 2021
Hi
what u can do over here is
add a,b,c... in a list e.g. list.append(vars..)
and then use the statement
newlist = map(lambda x:x.upper(),list)
Now ur newlist will contain the modified list.
HOPE THIS THE BETTER SOLUTION TO UR PROBLEM
Quoting ke******@dnr.state.mn.us:
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'
list = [a, b, c]
for i in range(len(list)): ... list[i] = list[i].upper()
... [a, b, c] = list
a 'ONE'

or, better:
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. Any suggestions for a better way to do this?

--Ken

--
http://mail.python.org/mailman/listinfo/python-list

--
Regards,
Jatinder Singh

Everyone needs to be loved... especially when they do not deserve it.
Jul 19 '05 #2
> >>> 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]

Jul 19 '05 #3
ke******@dnr.state.mn.us 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:

[snip]
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:
newList.append(item.upper())

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.

-Peter
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Fuzzyman | last post: by
33 posts views Thread by Arthur | last post: by
19 posts views Thread by Jim | last post: by
10 posts views Thread by s.lipnevich | last post: by
6 posts views Thread by MackS | last post: by
19 posts views Thread by bvdp | last post: by
7 posts views Thread by George Young | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.