mass editing for keys in a dictionary
Amit Khemka
khemkaamit at gmail.com
Fri Sep 14 03:08:09 EDT 2007
On 9/14/07, james_027 <cai.haibin at gmail.com> wrote:
> hi,
>
> How could I transform something like this
>
> dict_1 = {'customer_id':1, 'item_id':3, amount:100}
>
> into
>
> dict_2 = {'customer':1, 'item':3, amount:100}
A one liner would be :
>>> dict_2 = dict([(k.split('_')[0], v) for (k,v) in dict_1.iteritems()])
It would split the keys by '_' and use first part of split as the new key !
You must be aware be careful in cases where there are keys like
'customer_1' , 'customer_2', ...
Cheers,
--
----
Amit Khemka
website: www.onyomo.com
wap-site: www.owap.in
More information about the Python-list
mailing list