Making a set from dictionary values

后端 未结 2 699
旧巷少年郎
旧巷少年郎 2021-01-12 10:29

I want to create a set from the values of an existing dict

def function(dictionary):
    ... 
    return set_of_values

Say my dictionary lo

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-12 10:53

    For Python:

    set(d.values())
    

    Equivalent on Python 2.7:

    set(d.viewvalues())
    

    If you need a cross-compatible Python 2.7/3.x code:

    {d[k] for k in d}
    

提交回复
热议问题