Michael come lately
Michael come lately

Reputation: 9333

set of keys for nested dictionaries

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?

Upvotes: 0

Views: 438

Answers (3)

vaultah
vaultah

Reputation: 46533

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]}).

Upvotes: 6

chepner
chepner

Reputation: 531055

I would use the itertools module, specifically the chain class.

>>> x = {1: {2: 4, 3: 6}, 5: {2:6, 10: 25, 14: 12}}
>>> from itertools import chain
>>> set(chain.from_iterable(x.itervalues()))
set([2, 3, 10, 14])

Upvotes: 4

Ajax1234
Ajax1234

Reputation: 71451

You can use dict.items():

x = {1: {2: 4, 3: 6}, 5: {2:6, 10: 25, 14: 12}}
final_x = set(i for b in [b.keys() for i, b in x.items()] for i in b)

Output:

set([2, 3, 10, 14])

Upvotes: -1

Related Questions