Skip to main content

Python Sets

A set lets a single variable hold several items at once.

Python has four built-in collection types: List, Tuple, Dictionary and Set. Each one behaves differently and suits different jobs.

What makes a set special is that it is unordered, unchangeable* and unindexed.

Note: The "unchangeable" part applies to the existing items: you are still free to add new ones or remove old ones.

To write a set, put its items between curly brackets.

Example

Build a set and print it:

Prompt
Create a Python set with the items "apple", "banana", and "cherry". Print the set.
Output: complete Python program

Set intro

main.py
myset = {"apple", "banana", "cherry"}

Set Items

Three rules describe set items: they have no order, they cannot be changed, and no value can appear twice.


Unordered

A set keeps its items in no particular sequence.

Because of that, they may show up in a different arrangement each time you use the set, and you cannot reach one through an index or a key.


Unchangeable

After a set exists, the items inside it stay as they are; you cannot edit any of them.

The set itself can still grow or shrink, though, since adding and removing items is allowed.


Duplicates Not Allowed

Every value in a set must be unique, so two equal items can never sit side by side.

Note: To a set, True and 1 count as one value, so the second is dropped as a duplicate.

Note: The same goes for False and 0, which a set also treats as one value.

Example

The second "apple" is simply dropped:

Prompt
Create a Python set with duplicate values: "apple", "banana", "cherry", "apple". Print the set to show that duplicates are removed.
Output: complete Python program

Example

Here True and 1 collapse into one item:

Prompt
Create a set containing "apple", "banana", "cherry", True, 1, and 2. Print the set to show that True and 1 are treated as duplicates.
Output: complete Python program

Example

Here False and 0 collapse into one item:

Prompt
Create a set containing "apple", "banana", "cherry", False, True, and 0. Print the set to show that False and 0 are treated as duplicates.
Output: complete Python program

Get the Length of a Set

The len() function tells you how many items a set holds.

Example

Count the items in a set:

Prompt
Create a set with three items and use the len() function to get the number of items. Print the result.
Output: complete Python program

Set Items - Data Types

Set items are not limited to one data type.

You can even mix several types in one set:

Example

One set of strings, one of integers and one of booleans:

Prompt
Create three sets: one with strings, one with integers, and one with booleans. Show that set items can be of any data type.
Output: complete Python program

Example

Strings, integers and booleans together in one set:

Prompt
Create a set that contains different data types: a string, an integer, and a boolean. Show that a set can contain mixed data types.
Output: complete Python program

type()

Python treats every set as an object, and the name of its data type is 'set':

Example

Ask Python what type a set is:

Prompt
Create a set and use the type() function to check its data type. Print the result.
Output: complete Python program

The set() Constructor

Curly brackets are not the only option: the set() constructor builds a set too.

Example

Build a set with the set() constructor:

Prompt
Use the set() constructor to create a set with the items "apple", "banana", and "cherry". Print the set.
Output: complete Python program

Python Collections (Arrays)

The Python language gives you four collection data types:

  • List keeps its order and can be changed. Duplicate members are fine.
  • Tuple keeps its order but cannot be changed. Duplicate members are fine.
  • Set has no order or index, and its items are unchangeable*. Every member is unique.
  • Dictionary keeps its order** and can be changed. Every member is unique.

*You cannot edit existing set items, although adding and removing them is allowed.

**Dictionaries became ordered in Python version 3.7; up to and including Python 3.6 they were unordered.

Knowing what each type can and cannot do helps you pick well. The right choice for your data can preserve its meaning and may make your program more efficient or secure.