> ## 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 Del Statement

The `del` statement is a built-in Python keyword used to remove bindings between names (references) and objects within a specific namespace, or to remove items and attributes from mutable objects. It does not directly delete objects from memory; rather, it removes the reference to the object, which decrements the object's reference count. Actual memory deallocation is handled subsequently by Python's garbage collector when an object's reference count reaches zero.

## Syntax

The formal syntax for the `del` statement accepts a comma-separated list of targets:

```python theme={"dark"}
del target_list
```

A target can be an identifier (variable name), an attribute reference, a subscription (index/key), or a slicing.

## Target Mechanics

The behavior of the `del` statement changes depending on the nature of the target being evaluated.

### 1. Identifier (Name) Deletion

When the target is an identifier, `del` removes the binding of that name from the local or global namespace. Attempting to access the name after deletion raises a `NameError`.

```python theme={"dark"}
x = 100
y = x

del x  # Removes the name 'x' from the namespace

# print(x) -> Raises NameError


# The integer object 100 persists in memory because 'y' still references it.
```

If the identifier is located in a local scope (like inside a function), it is removed from the local namespace. To delete a global variable from within a local scope, the name must be declared with the `global` statement first.

Conversely, attempting to delete a variable explicitly declared as `nonlocal` is strictly prohibited. Python's scoping rules do not allow removing bindings from an enclosing closure scope via `del`, and doing so will raise a `SyntaxError` (`SyntaxError: cannot delete nonlocal`).

### 2. Subscription and Slicing Deletion

When the target is a subscription (an index or dictionary key) or a slice, the `del` statement delegates the operation to the container object by invoking its `__delitem__(key)` magic method.

```python theme={"dark"}

# Subscription deletion (Lists)
sequence = [10, 20, 30, 40]
del sequence[1]  # Invokes sequence.__delitem__(1)


# Slicing deletion
del sequence[1:3] # Invokes sequence.__delitem__(slice(1, 3))


# Subscription deletion (Dictionaries)
mapping = {'key1': 'value1', 'key2': 'value2'}
del mapping['key1'] # Invokes mapping.__delitem__('key1')
```

### 3. Attribute Deletion

When the target is an attribute reference, `del` delegates the operation to the parent object by invoking its `__delattr__(name)` magic method. This removes the attribute from the object's internal `__dict__` (or `__slots__`).

```python theme={"dark"}
class Configuration:
    def __init__(self):
        self.host = "localhost"
        self.port = 8080

config = Configuration()
del config.port  # Invokes config.__delattr__('port')
```

## Recursive Target Unpacking

The `target_list` can contain nested tuples or lists. The `del` statement unpacks these recursively, deleting each bound target from left to right.

```python theme={"dark"}
a, b, c = 1, 2, 3


# The following statement deletes 'a' and 'b', but leaves 'c' intact.
del (a, b) 


# Equivalent to:

# del a

# del b
```

## Restrictions

* You cannot delete literal values, expressions, or the results of function calls. `del` strictly requires a valid reference target.
* Attempting to delete a *subscription* or *slice* of an immutable sequence (e.g., `del my_tuple[0]`) will raise a `TypeError`, as immutable types do not implement `__delitem__`. However, deleting the identifier bound to the immutable sequence itself (e.g., `del my_tuple`) is perfectly valid, as this operates on the namespace reference rather than the underlying object.

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