many thanks for this to naveen and others who responded. 

On Thu, 22 Jul 2010 15:54:14 +0530  wrote
>I suggest that in such cases, avoid the temptation to do something

clever. If it was difficult for you to write the code, it will be even

more difficult to read and understand it.

Unless there is a very good reason, write the simplest, most braindead code.



xdictstart={}

xdictend={}

for item in sorted(x):

 xdictend[item[0]] = item[1]

 if not item[0] in xdictstart

  xdictstart[item[0]] = item[1]







On Thu, Jul 22, 2010 at 3:21 PM, Vikram  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.

>

> -------

> 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}

>

> _______________________________________________

> BangPypers mailing list

> BangPypers@python.org

> http://mail.python.org/mailman/listinfo/bangpypers

>

_______________________________________________

BangPypers mailing list

BangPypers@python.org

http://mail.python.org/mailman/listinfo/bangpypers

_______________________________________________
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers

Reply via email to