I have a couple lines to populate a set
.
x = {1: {2: 4, 3: 6}, 5: {2:6, 10: 25, 14: 12}}
keys = set()
for y in x:
for z in x[y]:
keys.add(z)
# keys is now `set([2, 3, 10, 14])`
I can't shake the feeling that I can do this better, but nothing I come up with is seems great. Most implementations build a list
first, which is annoying. There are lots of x
in y
, and most y
have the same z
.
# Builds a huuuuge list for large dicts.
# Adapted from https://stackoverflow.com/a/953097/241211
keys = set(itertools.chain(*x.values()))
# Still builds that big list, and hard to read as well.
# I wrote this one on my own, but it's pretty terrible.
keys = set(sum([x[y].keys() for y in x], []))
# Is this what I want?
# Finally got the terms in order from https://stackoverflow.com/a/952952/241211
keys = {z for y in x for z in x[y]}
Is the original code the "most pythonic" or is one of the one-liners better? Something else?
I would use
{k for d in x.itervalues() for k in d}
itervalues()
(just values()
in Python 3) doesn't build a list, and this solution doesn't involve dictionary lookup (in contrast to {z for y in x for z in x[y]}
).