interleaving dictionary values

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Tue Nov 21 12:17:41 EST 2006


d = {"a": [1, 2, 3], "b": [4, 5], "c": [7, 8, 9]}

# Simple solution:
result = []
for l in d.itervalues():
    result.extend(l)
print result

# Alternative:
from itertools import chain
print list( chain(*d.itervalues()) )

I don't know what's the faster solution, the first one seem more
readable and it's probably fast enough.

Bye,
bearophile




More information about the Python-list mailing list