<br />
<br />
Suppose you have the following list:<br />
<br />
>>> x =[['cat',10],['cat',20],['cat',30],['dog',5],['dog',1],['dog',3]]<br />
<br />
My problem is that i wish to obtain the following two dictionaries:<br />
xdictstart = {'cat':10, 'dog':1}<br />
xdictend = {'cat':30, 'dog':5}<br />
<br />
Any nice way to do the above? Thanks.<br />
<br />
-------<br />
Those interested in the above problem may consider the following code (which does not actually do what i want):<br />
<br />
>>> xdictend = dict(x)<br />
>>> xdictend<br />
{'dog': 3, 'cat': 30}<br />
<br />
>>> x<br />
[['cat', 10], ['cat', 20], ['cat', 30], ['dog', 5], ['dog', 1], ['dog', 3]]<br />
>>> xdictstart = {}<br />
>>> map(xdictstart.setdefault, *zip(*x))<br />
[10, 10, 10, 5, 5, 5]<br />
>>> xdictstart<br />
{'dog': 5, 'cat': 10}<br />
<br />
<br><Table border=0 Width=644 Height=57 cellspacing=0 cellpadding=0 style="font-family:Verdana;font-size:11px;line-height:15px;"><TR><td><A HREF="http://sigads.rediff.com/RealMedia/ads/click_nx.ads/www.rediffmail.com/signatureline.htm@Middle?" target="_blank"><IMG SRC="http://sigads.rediff.com/RealMedia/ads/adstream_nx.ads/www.rediffmail.com/signatureline.htm@Middle"></A></td></TR></Table>