[BangPypers] list problem

Vikram kpguy at rediffmail.com
Thu Jul 22 11:51:38 CEST 2010


Suppose you have the following list:

>>> x =[['cat',10],['cat',20],['cat',30],['dog',5],['dog',1],['dog',3]]

My problem is that i wish to obtain the following two dictionaries:
xdictstart = {'cat':10, 'dog':1}
xdictend = {'cat':30, 'dog':5}

Any nice way to do the above? Thanks.

-------
Those interested in the above problem may consider the following code (which does not actually do what i want):

>>> xdictend = dict(x)
>>> xdictend
{'dog': 3, 'cat': 30}

>>> x
[['cat', 10], ['cat', 20], ['cat', 30], ['dog', 5], ['dog', 1], ['dog', 3]]
>>> xdictstart = {}
>>> map(xdictstart.setdefault, *zip(*x))
[10, 10, 10, 5, 5, 5]
>>> xdictstart
{'dog': 5, 'cat': 10}



More information about the BangPypers mailing list