Is there something similar to list comprehension in dict?

Peng Yu pengyu.ut at gmail.com
Fri Nov 20 04:18:04 CET 2009


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]



More information about the Python-list mailing list