Skip to main content

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.

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

# 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.
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.
container = [1, 2, 3]
del container[:]


# container is now []

# container.__delitem__(slice(None, None, None)) is executed
Master Python with Deep Grasping Methodology!Learn More