How to retrieve an element from a set without removing it?

前端 未结 14 2654
孤城傲影
孤城傲影 2020-12-07 07:17

Suppose the following:

>>> s = set([1, 2, 3])

How do I get a value (any value) out of s without doing s.pop()

14条回答
  •  天涯浪人
    2020-12-07 07:45

    You can unpack the values to access the elements:

    s = set([1, 2, 3])
    
    v1, v2, v3 = s
    
    print(v1,v2,v3)
    #1 2 3
    

提交回复
热议问题