set() Set constructor
Last updated: 2026-09-22
set() Set constructor
set() returns a mutable set; with no args returns an empty set; with an iterable returns the deduped set.
| Category | Built-in Functions |
|---|---|
| Kind | Built-in Function |
| Python Version | all |
📝 Syntax
set([iterable])
⚙️ Parameters
| iterable Optional | An iterable; omitted gives an empty set.(Default:None) |
|---|
Returns:A mutable set of the deduplicated items; an empty set if called with no args.
💥 Raises
TypeError— Raised when the iterable is not iterable.
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
['a', 'b', 'c', 'd', 'r']
[1, 2]
🏷️ Related Built-in Functions
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat is the difference between set() and {}?
Aset() is an empty set, while {} is an empty dict! An empty set can only be created with set(); {} cannot represent an empty set.
QWhat does set([1, 2, 1, 2]) return?
AIt returns {1, 2} — a set automatically deduplicates and is unordered; this is also the classic 'list dedup' pattern list(set(lst)).
QWhy does set([1, [2]]) raise a TypeError?
ASet elements must be hashable, and a list is mutable and therefore unhashable; elements can only be immutable types like int/str/tuple/frozenset.
QDoes a set preserve insertion order?
ANo, a set is unordered; for ordered deduplication use dict.fromkeys(lst) (dicts keep insertion order in Python 3.7+).