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.

CategoryBuilt-in Functions
KindBuilt-in Function
Python Versionall

📝 Syntax

set([iterable])

⚙️ Parameters

iterable OptionalAn 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

▶ Example

Edit the code, press Run, and see the result in the output panel:

Output:

['a', 'b', 'c', 'd', 'r']
[1, 2]
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+).
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏