Dict comp help

Leonard, Arah Arah.Leonard at bruker-axs.com
Thu Jan 24 22:39:37 CET 2013


> Hi,
> Slightly different take on an old problem, I have a list of dicts, I need to build one dict from this based on two values from each dict in the list. Each of the dicts in the list have similar key names, but values of course differ.
> 
> 
> [{'a': 'xx', 'b': 'yy', 'c': 'zz'},  {'a': 'dd', 'b': 'ee', 'c': 'ff'}]
> 
> 
> { 'xx': 'zz', 'dd': 'ff'}
> 
> 
> Anyone have insight on how to pull this off?
> 
> 
> Thanks!
> jlc
> 

listy = [{'a':'xx', 'b':'yy', 'c':'zz'}, {'a':'dd', 'b':'ee','c':'ff'}]
kryten = {}
keys = []
for l in listy:
	for key in l.keys():
		if key not in keys: keys.append(key)
for key in keys:
	kryten[key] = ''
	for l in listy:
		kryten[key] += l.has_key(key) and l[key] or ''
print kryten



More information about the Python-list mailing list