30

I would like to get an element from a frozenset (without modifying it, of course, as frozensets are immutable). The best solution I have found so far is:

s = frozenset(['a'])
iter(s).next()

which returns, as expected:

'a'

In other words, is there any way of 'popping' an element from a frozenset without actually popping it?

7
  • 4
    I think your method is as good as any. If you want a random element you might check out random.sample(fset, 1).
    – bbayles
    Jul 23, 2013 at 4:52
  • Why do you want to pop the element, because it's arbitrary?
    – martineau
    Jul 23, 2013 at 5:16
  • 1
    I just want to get some arbitrary element from a frozenset. I shouldn't have used the word pop since the set remains unchanged. It is similar to peeking the first element of a stack without popping it.
    – ablondin
    Jul 23, 2013 at 5:32
  • 1
    That's what I use (but with the next builtin instead of the method). Jul 23, 2013 at 5:58
  • 1
    Don't use the method .next(). There is a next() built-in function since at least python2.6 and using it means that your code will work also in python3 where the next method was renamed __next__.
    – Bakuriu
    Jul 23, 2013 at 9:32

4 Answers 4

24

If you know that there is but one element in the frozenset, you can use iterable unpacking:

s = frozenset(['a'])
x, = s

This is somewhat a special case of the original question, but it comes in handy some times.

If you have a lot of these to do it might be faster than next(iter..:

>>> timeit.timeit('a,b = foo', setup='foo = frozenset(range(2))', number=100000000)
5.054765939712524
>>> timeit.timeit('a = next(iter(foo))', setup='foo = frozenset(range(2))', number=100000000)
11.258678197860718
2
  • ValueError: too many values to unpack on set with multiple elements.
    – A T
    Dec 27, 2016 at 16:19
  • 2
    as said, this only works with exactly one element in the frozenset. Jan 10, 2017 at 16:29
20

(Summarizing the answers given in the comments)

Your method is as good as any, with the caveat that, from Python 2.6, you should be using next(iter(s)) rather than iter(s).next().

If you want a random element rather than an arbitrary one, use the following:

import random
random.sample(s, 1)[0]

Here are a couple of examples demonstrating the difference between those two:

>>> s = frozenset("kapow")
>>> [next(iter(s)) for _ in range(10)]
['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
>>> import random
>>> [random.sample(s, 1)[0] for _ in range(10)]
['w', 'a', 'o', 'o', 'w', 'o', 'k', 'k', 'p', 'k']
2
12

You could use with python 3:

>>> s = frozenset(['a', 'b', 'c', 'd'])
>>> x, *_ = s
>>> x
'a'
>>> _, x, *_ = s
>>> x
'b'
>>> *_, x, _ = s
>>> x
'c'
>>> *_, x = s
>>> x
'd'
0
1

Using list:

list_ = list(frozenset("Benyamin"))

Using generator:

def get_frozenset_elements(frozen_set):
    for i in frozen_set:
        yield i

gen_ = get_frozenset_elements(frozenset("Benyamin"))
next(gen_)
next(gen_)
next(gen_)
...

Using iterator:

iter_ = iter(frozenset("Benyamin"))
next(gen_)
next(gen_)
next(gen_)
...

[NOTE]:

Difference between Python's Generators and Iterators

1
  • 1
    The first of these is just a complicated way to write list(my_frozenset), which is quite wasteful for large sets. The second is just a complicated way to write next(iter(my_frozenset)), which is already the accepted answer.
    – wchargin
    Jan 6, 2020 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.