> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Python In

The `in` operator is a membership operator that evaluates whether a specified element exists within an iterable, sequence, or collection. It returns a boolean value (`True` if the element is found, `False` otherwise). Its negated counterpart is `not in`.

```python theme={"dark"}
element in collection
element not in collection
```

## Underlying Implementation

When the Python interpreter encounters the `in` operator, it delegates the evaluation to the collection's data model. Specifically, the expression `x in y` translates to a call to the `__contains__()` dunder (magic) method of the collection `y`.

```python theme={"dark"}

# The standard expression:
x in y


# Is internally evaluated as:
type(y).__contains__(y, x)
```

## Fallback Protocol

If the target object does not implement the `__contains__()` method, Python does not immediately fail. Instead, it falls back to iteration protocols in the following order:

1. **`__iter__()`**: Python attempts to iterate over the object, checking each item for *identity* first, and then *equality* (equivalent to `x is item or x == item`). It short-circuits and returns `True` upon the first match. This identity-first check is a critical semantic detail. For example, using a custom generator that lacks a `__contains__` method demonstrates how identity supersedes equality:

```python theme={"dark"}
x = float('nan')

def custom_iterable():
    yield x


# Evaluates to True because 'x is x', 

# even though 'float('nan') == float('nan')' is False.
x in custom_iterable() 
```

2. **`__getitem__()`**: If the object does not implement `__iter__()` but implements sequence semantics, Python attempts to access elements using sequential integer indices starting from `0` until a match is found or an `IndexError` is raised.
3. **`TypeError`**: If none of these methods are implemented, Python raises a `TypeError`, indicating the right-hand operand is not iterable.

## Algorithmic Complexity

The time complexity of the `in` operator is strictly dependent on the underlying data structure of the collection being queried:

* **Sets and Dictionaries (`set`, `dict`):** Average case **O(1)**, worst case **O(n)**. The `in` operator computes the hash of the target element and checks the corresponding bucket in the hash table. Note that for dictionaries, `in` evaluates membership against the *keys*, not the values.
* **Lists and Tuples (`list`, `tuple`):** **O(n)**. Python performs a linear scan, evaluating identity and then equality (`x is item or x == item`) for each element sequentially until a match is found or the sequence is exhausted.
* **Strings (`str`):** Average case **O(n)**, worst case **O(n \* m)** (where *n* is the length of the main string and *m* is the length of the substring). Unlike other sequences where `in` checks for exact element matches, applying `in` to strings invokes a substring search algorithm (typically a variation of Boyer-Moore-Horspool in CPython).

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Python Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
