Hello,
 
I was wondering why, in the code below, dictionary 'a' can be populated while 
dictionary 'b' can't.
b.get(k, []) will not return the default value [], but 'None' if k is not 
present in 'b'. Why?
Of course, one could use try-except to do this, but I'd just like to understand 
why the .get() version does not work.
 
Thanks!
 
import random
 
a = {}
for i in range(100):
  k = random.randint(1, 10)
  a[k] = a.get(k, 0) + 1
print a
 
b = {}
process = lambda k: k**2
for i in range(100):
  k = random.randint(1, 10)
  v = process(k)
  b[k] = b.get(k, []).append(v) # <--- error!
print b



Regards,
Albert-Jan


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a 
fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to