[Tutor] Change dictionary value depending on a conditional statement.

bob gailer bgailer at alum.rpi.edu
Mon Feb 11 19:05:38 CET 2008


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



More information about the Tutor mailing list