Python sets are a built-in data type used to store an unordered collection of unique elements. They are ideal for tasks like removing duplicates from data, performing fast membership testing, and executing mathematical set operations.

  • Creation: Use curly braces {} for non-empty sets (e.g., my_set = {1, 2, 3}) or the set() constructor for empty sets or conversions from other iterables (e.g., set([1, 2, 2]){1, 2}).

  • Key Properties:

    • No duplicates: Automatically removes repeated values.

    • Unordered: Elements have no fixed position; order may vary between runs.

    • Unindexed: Cannot access elements by index or key.

    • Mutable: Can add or remove elements after creation.

    • Hashable elements only: Only immutable types (e.g., integers, strings, tuples) can be stored; lists or dictionaries are not allowed.

  • Common Operations:

    • Add/Remove: Use add() to add an element, remove() (raises error if missing), or discard() (safe removal).

    • Set Math: Use union(), intersection(), difference(), and symmetric_difference() for mathematical operations.

    • Membership Testing: Use in or not in for O(1) lookup speed, much faster than lists.

  • Important Note: Use set() to create an empty set—{} creates an empty dictionary, not a set.

Sets are especially powerful for performance-critical operations involving uniqueness and fast lookups.

🌐
W3Schools
w3schools.com › python › python_sets.asp
Python Sets
Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary, all with different qualities and usage.
🌐
Python documentation
docs.python.org › 3 › tutorial › datastructures.html
5. Data Structures — Python 3.14.3 documentation
Python also includes a data type for sets. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries.
Discussions

Python 3.14, due out later this year, is set to receive a new type...
Python 3.14, due out later this year, is set to receive a new type of interpreter that can boost performance by up to 30% with no changes to existing code. 👨‍💻 (infoworld.com) ... Post a comment! ... [+]NakedNick_ballin comment score below threshold-17 points-16 points-15 points 1 month ago (3 children) ... Use of this site constitutes acceptance of our User Agreement and Privacy Policy. © 2025 reddit ... More on old.reddit.com
🌐 r/programming
How can I set up a YouTube-style video server?
Assuming the video file is structured correctly, and you don't care about transcoding, it need not be more complex than exposing the file somewhere as a static file and ensuring the static web server supports satisfying partial ranges (all servers you've probably heard of will, e.g. Apache, nginx, lighttpd, etc). As for what "structured correctly" means, I'm not entirely sure, but for example, classic AVI files would store an index at the end of the file that players must read before seeking will function. I'm not sure what the requirements are on MP4 files. More on reddit.com
🌐 r/Python
8
0
January 3, 2016
Python printing without page feed & set font, font size
I'm working on an application that needs to print to an ancient dot matrix printer. Get out the manuals or find them only and you'll probably discover that there's a really simple serial command language you can use to talk to the printer directly, no driver necessary, just over PySerial. More on reddit.com
🌐 r/learnpython
3
3
December 28, 2018
Why does python rearrange the elements of a set in this case?
Sets and dictionaries are hashed, so they are in "hash order". Use an ordered dictionary if you want to keep the order. More on reddit.com
🌐 r/learnpython
12
3
June 17, 2019
🌐
GeeksforGeeks
geeksforgeeks.org › python › sets-in-python
Sets in Python - GeeksforGeeks
A Set in Python is used to store a collection of items with the following properties.
Published   January 10, 2026
🌐
Python Morsels
pythonmorsels.com › practical-uses-of-sets
Practical uses of sets - Python Morsels
April 15, 2025 - Sets are unordered collections of values that are great for removing duplicates, quick containment checks, and set operations.
🌐
Wikibooks
en.wikibooks.org › wiki › Python_Programming › Sets
Python Programming/Sets - Wikibooks, open books for an open world
October 29, 2005 - Initially this implementation had to be imported from the standard module set, but with Python 2.6 the types set and frozenset became built-in types. A set is an unordered collection of objects, unlike sequence objects such as lists and tuples, in which each element is indexed.
🌐
Replit
replit.com › home › discover › how to append to a set in python
How to append to a set in Python | Replit
3 days ago - This example shows how you can easily convert a list into a set. The set() constructor takes an iterable, like the page_visitors list, and builds a new set from its contents. During this conversion, Python's set data structure enforces its rule of only storing unique elements.
🌐
docs.python.org
docs.python.org › 3 › library › sets.html
Built-in Types — Python 3.7.3 documentation
The C implementation of Python makes the list appear empty for the duration, and raises ValueError if it can detect that the list has been mutated during a sort. Tuples are immutable sequences, typically used to store collections of heterogeneous data (such as the 2-tuples produced by the enumerate() built-in). Tuples are also used for cases where an immutable sequence of homogeneous data is needed (such as allowing storage in a set ...
Find elsewhere
🌐
Python Land
python.land › home › python data types › python set: the why and how with example code
Python Set: The Why And How With Example Code • Python Land Tutorial
September 16, 2025 - Sets only contain unique elements and we can create a set by giving the set() function a list. Those are all the ingredients you need to deduplicate a list. Deduplication is the process of removing duplicates and converting a list to a set is by far to easiest way to do this in Python:
🌐
Datamentor
datamentor.io › python › set
Python Set (With Examples)
In Python, a set is a collection of unordered items. Every item of a set is unique (no duplicates) and immutable.
🌐
freeCodeCamp
freecodecamp.org › news › python-set-how-to-create-sets-in-python
Python Set – How to Create Sets in Python
June 30, 2022 - You can use sets in Python to store a collection of data in a single variable. Each of the built-in data structures in Python like lists, dictionaries, and tuples have their distinguishing features.
🌐
Real Python
realpython.com › python-sets
Sets in Python – Real Python
May 5, 2025 - You'll revisit the definition of unordered, unique, hashable collections, how to create and initialize sets, and key set operations. Python’s built-in set data type is a mutable and unordered collection of unique and hashable elements.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-set-function
set() Function in python - GeeksforGeeks
3 weeks ago - set() function in Python is used to create a set, which is an unordered collection of unique elements.
🌐
Hyperskill
hyperskill.org › university › python › set-and-set-in-python
Set and set() in Python
December 25, 2025 - Difference: Find elements in one set but not in another. ... In Python, a set is a built-in data structure used to store a collection of unique items. It is mutable, meaning its elements can be modified, added, or removed.
🌐
W3Schools
w3schools.com › python › python_sets_add.asp
Python - Add Set Items
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... Once a set is created, you cannot change its items, but you can add new items.
🌐
Python documentation
docs.python.org › 3 › library › functions.html
Built-in Functions — Python 3.14.3 documentation
1 week ago - For other containers see the built-in set, list, tuple, and dict classes, as well as the collections module. ... Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example, getattr(x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised. name need not be a Python identifier (see setattr()).
🌐
Programiz
programiz.com › python-programming › set
Python Set (With Examples)
Become a certified Python programmer. Try Programiz PRO! ... A set is a collection of unique data, meaning that elements within a set cannot be duplicated.
🌐
AskPython
askpython.com › home › python set – things you must know
Python Set - Things You MUST Know - AskPython
January 25, 2026 - Sets are Python’s built-in data structure for storing unique, unordered collections.
🌐
W3Schools
w3schools.com › python › python_sets_access.asp
Python - Access Set Items
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... You cannot access items in a set by referring to an index or a key.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-sets
Python Sets - GeeksforGeeks
Python set is an unordered collection of multiple items having different datatypes. In Python, sets are mutable, unindexed and do not contain duplicates.
Published   1 week ago