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

前端 未结 14 2624
孤城傲影
孤城傲影 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 08:05

    How about s.copy().pop()? I haven't timed it, but it should work and it's simple. It works best for small sets however, as it copies the whole set.

提交回复
热议问题