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")
>>> l
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]
>
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 ta
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':
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 cha
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 valu