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

前端 未结 14 2646
孤城傲影
孤城傲影 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:55

    Least code would be:

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

    Obviously this would create a new list which contains each member of the set, so not great if your set is very large.

提交回复
热议问题