Re: [Tutor] Change dictionary value depending on a conditional statement.
I don't think I saw anyone point this out yet, but, using "list" as a variable name is a bad idea, because it hides the list method. >>> x = list("abcdefg") >>> x ['a', 'b', 'c', 'd', 'e', 'f', 'g'] This works. You now have a variable named "x" that is a list. >>> list = list("hijklmnop") >>> list ['h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'] This works, sort of. You now have a variable named "list" that is a list. But since you reused the name "list," it can no longer point to the list function. >>> y = list("qrstuv") Traceback (most recent call last): File "", line 1, in TypeError: 'list' object is not callable And so this fails. On Tue, 12 Feb 2008, Norman Khine wrote: > Thank you all, very nice. > > Steve Willoughby wrote: > > Kent Johnson wrote: > >> Try > >>list.append({'id': 'name', 'link': ('YY','XX')[total > 0]}) > > > > I'd caution against that, though. It's clever and cute, sure, but the > > meaning of it is obfuscated enough to be unpythonic because [total > 0] > > as a subscript doesn't mean anything unless you know you're taking > > advantage of an implementation detail that booleans are 0 for false and > > 1 for true. No matter how reliable that fact may be, I don't think that > > value should be stuck into a numeric context like that. > > > >> Or, in Python 2.5, > >>list.append({'id': 'name', 'link': ('XX' if total > 0 else 'YY')}) > > > > This is much more clear, and would IMHO be fine. > > > > > > > > ___ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Change dictionary value depending on a conditional statement.
Thank you all, very nice. Steve Willoughby wrote: > Kent Johnson wrote: >> Try >>list.append({'id': 'name', 'link': ('YY','XX')[total > 0]}) > > I'd caution against that, though. It's clever and cute, sure, but the > meaning of it is obfuscated enough to be unpythonic because [total > 0] > as a subscript doesn't mean anything unless you know you're taking > advantage of an implementation detail that booleans are 0 for false and > 1 for true. No matter how reliable that fact may be, I don't think that > value should be stuck into a numeric context like that. > >> Or, in Python 2.5, >>list.append({'id': 'name', 'link': ('XX' if total > 0 else 'YY')}) > > This is much more clear, and would IMHO be fine. > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Change dictionary value depending on a conditional statement.
Kent Johnson wrote: > Try >list.append({'id': 'name', 'link': ('YY','XX')[total > 0]}) I'd caution against that, though. It's clever and cute, sure, but the meaning of it is obfuscated enough to be unpythonic because [total > 0] as a subscript doesn't mean anything unless you know you're taking advantage of an implementation detail that booleans are 0 for false and 1 for true. No matter how reliable that fact may be, I don't think that value should be stuck into a numeric context like that. > Or, in Python 2.5, >list.append({'id': 'name', 'link': ('XX' if total > 0 else 'YY')}) This is much more clear, and would IMHO be fine. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Change dictionary value depending on a conditional statement.
bob gailer wrote: > The terse version: > > list.append({'id': 'name', 'link': ('XX','YY')[total > 0]}) I think you have it backwards: In [1]: total=0 In [2]: ('XX','YY')[total > 0] Out[2]: 'XX' In [3]: total=1 In [4]: ('XX','YY')[total > 0] Out[4]: 'YY' Try list.append({'id': 'name', 'link': ('YY','XX')[total > 0]}) Or, in Python 2.5, list.append({'id': 'name', 'link': ('XX' if total > 0 else 'YY')}) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Change dictionary value depending on a conditional statement.
Norman Khine wrote: > Hello, > Is there a better way to do this: > > >>> list = [] > >>> total = 0 > >>> if total > 0: > ... x = {'id': 'name', 'link': 'XX'} > ... list.append(x) > ... else: > ... y = {'id': 'name', 'link': 'YY'} > ... list.append(y) > ... > > I would like to change the key 'link' value depending on the value of > 'total' so for example if total == 2, then append x else y to the list. > The terse version: list.append({'id': 'name', 'link': ('XX','YY')[total > 0]}) -- Bob Gailer 919-636-4239 Chapel Hill, NC ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor