How to say $a=$b->{"A"} ||={} in Python?

beginner zyzhu2000 at gmail.com
Fri Aug 17 05:16:32 CEST 2007


On Aug 16, 5:35 pm, beginner <zyzhu2... at gmail.com> wrote:
> Hi All.
>
> I'd like to do the following in more succint code:
>
> if k in b:
>     a=b[k]
> else:
>     a={}
>     b[k]=a
>
> a['A']=1
>
> In perl it is just one line: $a=$b->{"A"} ||={}.
>
> Thanks,
> Geoffrey

It looks like defaultdict is the solution for this kind of thing.
Thanks all for your help.




More information about the Python-list mailing list