Set Literal Values

There are no literal values for sets. A set value is created by using the set or frozenset factory functions. These can be applied to any iterable container, which includes any sequence, and also includes files.

We'll return to “iterable” when we look at the yield statement in Chapter 18, Generators and the yield Statement .

set( iterable ) → set

Transforms the given iterable (sequence, file or set) into a set.

>>> 
set( ("hello", "world", "of", "words", "of", "world") )

set(['world', 'hello', 'words', 'of'])

Note that we provided a six-tuple to the set function, and we got a set with the four unique objects. The set is shown with a list literal, to remind us that a set is mutable.

frozenset ( iterable ) → set

Transforms the given iterable (sequence, file or set) into an immutable frozenset.