Is there something similar to list comprehension in dict?

Andre Engels andreengels at gmail.com
Fri Nov 20 06:41:10 EST 2009


On Fri, Nov 20, 2009 at 4:18 AM, Peng Yu <pengyu.ut at gmail.com> wrote:
> I'm wondering if there is something similar to list comprehension for
> dict (please see the example code below).
>
>
> d = dict(one=1, two=2)
> print d
>
> def fun(d):#Is there a way similar to list comprehension to change the
> argument d so that d is changed?
>  d=dict(three=3)
>
> fun(d)
> print d
>
> def fun1(d):
>  d['one']=-1
>
> fun1(d)
> print d
>
>
> L = [1, 2]
> print L
>
> def fun2(L):#this doesn't have any effect on the argument L
>  L=[]
>
> fun2(L)
> print L#[1, 2]
>
> def fun3(L):# argument L is changed
>  L[:]=[1, 2, 3]
>
> fun3(L)
> print L#[1, 2, 3]
> --
> http://mail.python.org/mailman/listinfo/python-list
>

def fun(d):
   d.clear()
   d[three] = 3




-- 
André Engels, andreengels at gmail.com



More information about the Python-list mailing list