Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set and frozenset difference in implementation

Tags:

python

I checked on this link that set is mutable https://docs.python.org/3/library/stdtypes.html#frozenset while frozenset is immutable and hence hashable. So how is the set implemented in python and what is the element look up time? Actually I had a list of tuples [(1,2),(3,4),(2,1)] where each entry in the tuple is a id and I wanted to create a set/frozenset out of the this list. In this case the set should contain (1,2,3,4) as elements. Can I use frozenset to insert elements into it one by one from the list of tuples or I can only use a set?

like image 918
vkaul11 Avatar asked Jul 15 '13 02:07

vkaul11


People also ask

What is the difference between a set and Frozenset?

Frozen set is just an immutable version of a Python set object. While elements of a set can be modified at any time, elements of the frozen set remain the same after creation. Due to this, frozen sets can be used as keys in Dictionary or as elements of another set.

What is the difference between tuple and Frozenset?

tuples are immutable lists , frozensets are immutable sets . frozensets aren't indexed, but you have the functionality of sets - O(1) element lookups, and functionality such as unions and intersections. They also can't contain duplicates, like their mutable counterparts.

What is the use of Frozenset?

Definition and Usage The frozenset() function returns an unchangeable frozenset object (which is like a set object, only unchangeable).

Is frozen set faster than set?

Initially, I assumed that frozenset would provide a better lookup performance than set , as its immutable and thus could exploit the structure of the stored items. It seems that frozenset is actually slower regarding the lookup performance, both in CPython and in PyPy.


2 Answers

You can instantiate a frozenset from a generator expression or other iterable. It's not immutable until it's finished being instantiated.

>>> L = [(1,2),(3,4),(2,1)]
>>> from itertools import chain
>>> frozenset(chain.from_iterable(L))
frozenset([1, 2, 3, 4])

Python3.3 also has an optimisation that turns set literals such as {1, 2, 3, 4} into precomputed frozensets when used as the right-hand side of an in operator.

like image 88
John La Rooy Avatar answered Sep 29 '22 10:09

John La Rooy


Sets and frozensets are implemented the same way, as hashtables. (Why else would they require their elements to implement __hash__?) In fact, if you look at Objects/setobject.c, they share almost all their code. This means that as long as hash collisions don't get out of hand, lookup and deletion are O(1) and insertion is amortized O(1).

The usual way to create a frozenset is to initialize it with some other iterable. As gnibbler suggested, the best fit here would probably be itertools.chain.from_iterable:

>>> L = [(1,2),(3,4),(2,1)]
>>> from itertools import chain
>>> frozenset(chain.from_iterable(L))
frozenset([1, 2, 3, 4])
like image 43
user2357112 supports Monica Avatar answered Sep 29 '22 11:09

user2357112 supports Monica