sets

set

They are:

  • Unordered
  • Can store a mixture of types (like tuple)
  • Cannot have duplicates
  • Don’t have indices (so no SET[i]), and no slicing
  • Immutable elements (once you have an item in a set, it cannot be changed)
  • Uses hashing internally (so more efficient than list sometimes)

[!NOTE] True and 1 are the same thing in sets, so are False and 0, the reason should be pretty obvious.

You declare a set like this:

a = {1, 2, 3}
b = set(DATA) # DATA can be either a list or a tuple

c = set() # empty set

[!WARNING] Empty sets must be declared using the set() function instead of empty square brackets as those are reserved for generating an empty dict.

Create

a = {1, 2, 3}
a.add(4)

Read

a = {1, 2, 3}

for element in a:
    ...
a = {1, 2, 3}
iter_a = iter(a)
e = next(iter_a)

The only ways are basically using iterators

Delete

See the “Set Element Deletion” section in “deleting stuff” (Set Element Deletion).

frozenset

The truly immutable set.

#python #python/features