python 3.x - how to convert dict_values into a set -


i have dict contains sets values each key, e.g.

{'key1': {8772, 9605},'key2': {10867, 10911, 10917},'key3': {11749,11750},'key4': {14721, 19755, 21281}} 

now want put each value, i.e. set of ints set, wondering best way/most efficient way this.

{8772,9605,10867,10911,10917,11749,11750,14721,19755,21281} 

i tried retrieve values dict using dict.values(), returns dict_values object, making list, list(dict.values()) gave me list of sets, set(list(exact_dups.values())) threw me errors,

typeerror: unhashable type: 'set' 

update. forgot mention result set need maintain uniqueness, i.e. no duplicates.

you can set.union() , unpacked values:

set.union(*my_dict.values()) 

or can combine set.union() reduce:

reduce(set.union, my_dict.values()) 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -