in Membership test
Last updated: 2026-09-22
in Membership test
in checks containment; also drives for-loops.
| Category | Operators |
|---|---|
| Kind | Operator |
| Python Version | all |
📝 Syntax
x in container
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
True
True
True
💡 Related Cases
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`).