in Membership test

Last updated: 2026-09-22

in Membership test

in checks containment; also drives for-loops.

CategoryOperators
KindOperator
Python Versionall

📝 Syntax

x in container

▶ Example

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

Output:

True
True
True
More case studies coming soon — check back later.

❓ FAQ

QWhat does in check on a dictionary?
AOnly keys: `'k' in {'k': 1}` is `True`. To check a value use `'v' in d.values()`, and to check a key-value pair use `('k', 1) in d.items()`.
QDoes in do substring matching on strings?
AYes. `'x' in 'xyz'` is `True`, and `'yz' in 'xyz'` is also `True`. Note that the empty string `'' in 'abc'` is always `True`, because the empty string is a substring of every string.
QHow efficient is in for large containers?
A`list` and `tuple` are O(n) linear scans; `set` and `dict` are O(1) hash lookups. If you do frequent membership tests, converting the data to a set first is much faster.
QIs the in in a for loop the same thing as x in lst?
AIt is the same keyword but a different syntactic role: `for x in lst` is iteration syntax (takes items one by one), while `x in lst` is a membership-test expression (returns `True`/`False`).
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%

🙏 帮我们做得更好

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

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