[BangPypers] list problem

Anand Balachandran Pillai abpillai at gmail.com
Thu Jul 22 13:32:38 CEST 2010


On Thu, Jul 22, 2010 at 3:21 PM, Vikram <kpguy at rediffmail.com> wrote:

> 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.
>
>
Yes. Try this.

>>> x =[['cat',10],['cat',20],['cat',30],['dog',5],['dog',1],['dog',3]]
>>> x.sort()
>>> xdictstart = dict(reversed(x))
>>> xdictend = dict(x)
>>> xdictstart,xdictend
({'dog': 1, 'cat': 10}, {'dog': 5, 'cat': 30})

-- Anand


More information about the BangPypers mailing list