How to extract the member from single-member set in python?

前端 未结 6 1290
感动是毒
感动是毒 2020-12-23 18:46

I recently encountered a scenario in which if a set only contained a single element, I wanted to do something with that element. To get the element, I settled on this appro

6条回答
  •  梦毁少年i
    2020-12-23 19:12

    you can use element = tuple(myset)[0] which is a bit more efficient, or, you can do something like

    element = iter(myset).next()
    

    I guess constructing an iterator is more efficient than constructing a tuple/list.

提交回复
热议问题