[Tutor] Change dictionary value depending on a conditional statement.
Kent Johnson
kent37 at tds.net
Mon Feb 11 20:00:48 CET 2008
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
More information about the Tutor
mailing list