Python Set

#python #datastructure #programming #language #efficiency

  • They are two types of sets: set and frozenset
    • set: Not hashable, cannot be nested
    • frozenset: Hashable, we can have frozenset inside set
  • Take a look on both codes below
found = len(needles & haystack) # (1)

found = 0 # (2)
for n in needless:
	if n in haystack:
		found += 1
  • The code (1) has superior performance than the code (2).
  • To build a set
>>> s = {1} # faster than
>>> s = set([1,2,3])
  • Take a look on setcomp
  • Elements inside set need to be Hashable or has the methods __hash__ and __eq__
    • But the check of existence of element is fast
  • Sets have memory overhead
  • Sorting is not guaranteed in set, adding elements can change the order
  • Python Dictionary methods like keys() and items() implement some set methods like: &, |, - and ^.
Links to this page
#python #datastructure #programming #language #efficiency