set – an unordered collection of unique elements

The set data type is an unordered collection of unique elements. The collection is mutable insofar as elements can be added and removed from the set, but each element must itself be immutable, very much like the keys of a dictionary.

Sets are unordered groups of distinct elements as illustrated in the following image:


Figure 5.21: Set

Sets have a literal form very similar to dictionaries, again delimited by curly braces, but each item is a single object, rather than a pair joined by a colon:

>>> p = {6, 28, 496, 8128, 33550336}

Note that like a dictionary, the set is unordered:

>>> p
{33550336, 8128, 28, 496, 6}

Of course, sets have type set:

>>> type(p)
<class 'set'>

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset