> ## 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 Slicing

The `[:]` operator is a syntactic construct in Python used to perform a full slice on a sequence or collection. It evaluates to a `slice` object with its `start`, `stop`, and `step` attributes implicitly set to `None`, effectively instructing the interpreter to traverse the entire iterable from the first element to the last.

When the Python interpreter encounters the `[:]` syntax, it translates it into a built-in `slice` object.

```python theme={"dark"}
sequence[:]


# Under the hood, this is evaluated as:
sequence.__getitem__(slice(None, None, None))
```

The behavior of the `[:]` operator depends strictly on the context of its execution—specifically, whether it is used for retrieval (evaluation), mutation (assignment), or deletion.

## Retrieval Semantics (`__getitem__`)

When used in an expression to retrieve data, `[:]` invokes the object's `__getitem__` method. The memory allocation behavior during this operation depends on the mutability of the underlying sequence:

* **Mutable Sequences (`list`, `bytearray`):** The operation allocates a new object in memory and populates it with references to the items in the original sequence. This results in a **shallow copy**. The outer container is a distinct object with a new memory address, but the nested elements within it reference the exact same objects in memory as the original.
* **Immutable Sequences (`tuple`, `str`, `bytes`):** Because the sequence cannot be modified, CPython optimizes this operation by returning a reference to the exact same object rather than allocating a new distinct container.

```python theme={"dark"}

# Mutable sequence (list)
original_list = [1, [2, 3]]
sliced_list = original_list[:]

print(id(original_list) == id(sliced_list))       # False (Distinct outer containers)
print(id(original_list[1]) == id(sliced_list[1])) # True  (Shared inner references)


# Immutable sequence (tuple)
original_tuple = (1, 2, 3)
sliced_tuple = original_tuple[:]

print(id(original_tuple) == id(sliced_tuple))     # True  (CPython optimization)
```

*Note: For third-party array libraries like NumPy, `[:]` does not create a copy in memory. Instead, it returns a **view** of the original memory buffer.*

## Assignment Semantics (`__setitem__`)

When placed on the left-hand side of an assignment operator, `[:]` invokes the object's `__setitem__` method. This instructs Python to perform an **in-place mutation** of the existing object rather than rebinding the variable name to a new memory address.

The iterable on the right-hand side of the assignment will overwrite the existing elements within the original container. The container retains its original memory address (`id`), but its internal pointers are updated.

```python theme={"dark"}
target = [1, 2, 3]
memory_address = id(target)


# In-place mutation via slice assignment
target[:] = [7, 8, 9, 10]


# The variable 'target' still points to the exact same object in memory
print(id(target) == memory_address)  # True
```

## Deletion Semantics (`__delitem__`)

When combined with the `del` keyword, `[:]` invokes the `__delitem__` method. This clears all elements from the sequence in-place, leaving an empty container at the original memory address, which is functionally equivalent to calling `.clear()` on mutable collections.

```python theme={"dark"}
container = [1, 2, 3]
del container[:]


# container is now []

# container.__delitem__(slice(None, None, None)) is executed
```

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