lists and dictionaries

Bart Ogryczak B.Ogryczak at gmail.com
Thu Jul 12 16:14:40 CEST 2007


On 11 jul, 21:08, Ladislav Andel <lad... at iptel.org> wrote:
> Hi,
> I have a list of dictionaries.
> e.g.
> [{'index': 0, 'transport': 'udp', 'service_domain': 'dp0.example.com'},
> {'index': 1, 'transport': 'udp', 'service_domain': 'dp1.example.com'},
> {'index': 0, 'transport': 'tcp', 'service_domain': 'dp0.example.com'},
> {'index': 1, 'transport': 'tcp', 'service_domain': 'dp1.example.com'}]
>
> how could I make a new list of dictionaries which would look like:
> [{'transports': ['udp','tcp'], 'service_domain': 'dp0.example.com'},
> {'transports': ['udp','tcp'], 'service_domain': 'dp1.example.com'}]
>
> Could you help me, please?

doms = {}
for entry in oldList:
    doms.setdefault(entry['service_domain'],
[]).append(entry['transport'])
newList = [{'transports': t, 'service_domain': d} for d,t in
doms.iteritems()]





More information about the Python-list mailing list