- ● What are python sets
- ● Empty sets
- ● Length of a set
- ● Properties of a set
- ● Create set from iterables
- ● Set members must be immutable
- ● Membership of set members
- ● Set with boolean literals
- ● Set methods (seperate page)

Sets are one of the important data-structures in python.
We have studied set theories in our schooling.
Python sets are similar to mathematical sets.
Operations on python sets are also similar to
union, intersection etc. of mathematical sets.

In this page, we are going
to have the basic conceptions of python sets.

It is recommended to go through the details of
python lists and
tuples
before studying python sets.

**Note:** All the terms 'item', 'element' and 'member' have been used synonymously.
They refer to the member of a set.

- ● What are python sets
- ● Empty sets
- ● Length of a set
- ● Properties of a set
- ● Create set from iterables
- ● A set member must be immutable
- ● Membership of set members
- ● Set with boolean literals
- ● Set methods (seperate page)

A python set is an mutable, unordered collection of unique and heterogeneous elements.

Unlike list and tuples, an elements of a set has no particular position,
so indexing or slicing is not possible with set elements.

Duplicate elements are not allowed in a python set.

Set elements are kept within curly braces. {1, 2, 3} is a valid set literal. We can check this in our IDLE window.

In the following, we assign a set literal to a variable.

The order of items have changed in the output! We are just coming to this.

To create an empty set, we must use the set() function.

Once an empty set is created, we can add items to it with the .add() method.

Python len() function is used to find the length of an object. When the argument is a set object, len(set) simply returns the length of the given set. In other words, the number of items in the set is returned.

An empty set has a zero length. Note that the data-type of variable length is 'int'.

Sets are unordered. Items of a set has a no positional value or index. We enter set items in one order, and output may be of same or different order.

Note that in case of set s2, the order of items in the output differs from that of the input statement. Set items have no order.

All set items are **unique**.
Opposite to python lists and tuples, sets never contain duplicate items.

If we include a duplicate item, python will silently remove it.

In the above set the last item is a duplicate, hence not included.

This property of set is very useful if we require to maintain a collection where every item is to be unique, like the collection of employee id.

A set can contain items or elements that are of different data-types.

The above set contains 'float, 'str' and 'int' type objects.

A set is a mutable object. We can any time add or remove item to and from a set.

The set() function can be used to create a set from iterables like 'str', 'list', 'tuple', 'dict' type objects.

Note that the duplicate members in [3, 2, 1, 2, 3] have been omitted in the output set {1, 2, 3}.

Note that the duplicate characters in "book" have
been omitted in the output set {'b', 'k', 'o'}.

We have seen that a set can be created with the items of a list, tuple, dictionary or string. But a list itself cannot be an element of a set, because lists are mutable. This applies to dictionaries and sets too.

A tuple or string, being immutable, are allowed as a single element inside a set.

But a list or dictionary, being mutable, is not allowed as a single element inside a set. Even a set (being mutable) inside a set is not valid.

There is a difference between the statements

(a) include a collection(of items) in a set as a single member of the set.

(b) include the items of a collection in a set.

Collection in both the cases refers to lists,
tuples, sets, dictionaries and strings.

Let us go with **statement (a)** first.

Statement (a) translates as: "put a collection in a set(directly) as a single member".

This is not possible except for a tuple. A mutable collection/object
like a list, set or dictionary **cannot be** a member of a set.
Try the following assignments:

a = { [1, 2] }** # a list in a set, invalid**

a = { {1, 2} }** # a set in a set, invalid**

a = { {'a': 1} }** # a dict in a set, invalid**

All of the above three assignments will produce **TypeError**.

But an immutable object like a tuple or string **can be** a member of a set.
Try the following assignments:

a = { (1, 2) }** # a tuple in a set, valid**

a = { "wan" }** # a string in a set, valid**

Now we go to the details of **statement (b)**.

Statement (b) translates as: "put the items of a sequence in a set as individual members".

Observe the following assignments, all are valid.

**# items of a list in a set**

b = set( [1, 2] )** # {1, 2}**

**# items of a tuple in a set**

b = set( (1, 2) )** # {1, 2}**

**# items of a set in a set**

b = set( {1, 2} )** # {1, 2}**

**# keys of a dictionary in a set**

b = set( {1: 'a', 2: 'b'} )** # {1, 2}**

# characters of a string in a set

b = set( "abc" )** # {'a', 'b', 'c'}**

Python membership operators in and not in can be used to check whether an item is a member of a set or not.

Since the item "English" is a member of the set subjects, the membership expression "English" in subjects returns True. "Maths" is also a member of the set, so the expression "Maths" not in subjects returns False.

A set cannot contain both 0 and False values as members. Check this in our IDLE shell:

The member False has been omitted in the output set. This behaviour of python is somewhat unexpected, still we have to remember it. The following facts may be observed:

- False == 0
**# True** - int(False)
**# 0** - bool(0)
**# False** - True == 1
**# True** - int(True)
**# 1** - bool(1)
**# True**

Ok, we have to remember that if 0 and
False are entered as set items, the
False value is omitted, the 0 remains.

But another confusion here:

Here,
0 is omitted, the False value remains.

Out of 0 and False,
whichever value is found earlier is kept, the other is discarded.

Every assessment above are applicable to a set of 1 and True.

Without going to any confusion, we have to remember that
a set cannot contain both 0 and
False values; whichever literal is found eralier is kept,
the other is discarded.

Also a set cannot contain both 1 and
True values; whichever literal is found eralier is kept,
the other is discarded.

So, we should be careful while dealing with the integer and boolean literals 0, 1, False, True in the same set.

- 01 Python sets are unordered collection of uniq items.
- 02 Set items are heterogeneous.
- 03 Sets are mutable objects.
- 04 A set contains only immutable members.
- 03 Set has no indexing and slicing.

# Summary

--- x ---

Want to leave a message for me?