[Baypiggies] list to dictionary problem

Alexandre Conrad alexandre.conrad at gmail.com
Tue Jun 22 11:57:28 CEST 2010


Hello Vikram,

2010/6/22 Vikram <kpguy at rediffmail.com>
>
> Suppose i have this list:
>
> >>> a = [['cat',2],['cat',5],['cat',9],['dog',6]]
> >>> a
> [['cat', 2], ['cat', 5], ['cat', 9], ['dog', 6]]
>
> Now, there is a nice way to obtain the value 9.
>
> >>> z = dict(a)
> >>> z
> {'dog': 6, 'cat': 9}
>
> Is there any elegant way to extract the value 2? (Value corresponding to the first occurence of 'cat' in the 2-D list).

I guess I'd just reverse the list and apply the same technique as you proposed.

>>> a = [['cat',2],['cat',5],['cat',9],['dog',6]]
>>> a.reverse()
>>> z = dict(a)
>>> z
{'dog': 6, 'cat': 2}

Would that be suitable for you?

Regards,
--
Alex
twitter.com/alexconrad


More information about the Baypiggies mailing list