Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Python Data Structures and NumPy Memory Semantics

Tech 1

Mutable Sequences

Lists allow dynamic sizing and heterogeneous data storage. They are initialized using square brackets.

# Initialize container and modify contents
data = [10, 'text', 3.5]

data.append('added item')   # Add to end
data[0] = 20                # Modify by index
item_removed = data.pop()   # Remove last element

Multi-dimensional nesting is supported:

matrix = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
# Access nested element: row=0, col=0, val=0 -> 1
val = matrix[0][0][0]

Immutable Sequences

Tuples use parentheses for declaration. They are immutable; once created, elements cannot be altered or removed.

fixed_point = (1, 'x')
single_element = (99,)  # Comma required for single-item tuple

# Unpacking
x, y = fixed_point
# print(fixed_point[0]) = 1

Mappings

Dictionaries store key-value pairs. Keys must be hashable.

config_map = {}
settings = {'mode': 'dev', 'port': 8080}

# Access with error handling
print(settings.get('host', 'localhost'))

# Update or insert
settings['version'] = 1.0

# Remove entry
removed_val = settings.pop('port', None)

Unordered Sets

Sets hold unique, unordered elements. Useful for membership testing and mathematical set operations.

unique_ids = {1, 2, 3}

unique_ids.add(4)            # Add single item
unique_ids.update({5, 6})    # Add iterable
unique_ids.discard(3)        # Remove if exists (safe)

# Set Algebra
group_a = {1, 2, 3}
group_b = {3, 4, 5}

overlap = group_a & group_b          # Intersection: {3}
combined = group_a | group_b         # Union: {1, 2, 3, 4, 5}
diff = group_a - group_b             # Difference: {1, 2}
sym_diff = group_a ^ group_b         # Symmetric Difference: {1, 2, 4, 5}

Numerical Arrays (NumPy)

NumPy arrays enforce a single data type and offer contiguous memory allocation for performance. Operations are vectorized.

import numpy as np

raw_list = [[1, 2], [3, 4]]
arr = np.array(raw_list)

# Pre-initialized arrays
zeros_mat = np.zeros((2, 2))
ones_vec = np.ones(5)
rng_range = np.arange(0, 10, 2)

# Arithmetic broadcasts over the whole array
result_add = arr + 5
result_mul = arr * 2

# Matrix multiplication (dot product)
mat_A = np.array([[1, 2], [3, 4]])
mat_B = np.array([[5, 6], [7, 8]])
matrix_prod = np.dot(mat_A, mat_B)

# Indexing allows slicing across multiple dimensions in one bracket call
chunk = arr[0, 0]

Array Internals and Copying

A NumPy ndarray object maintains references to:

  1. Data buffer pointer
  2. Data type descriptor (dtype)
  3. Shape metadata
  4. Strides (byte steps between dimensions)

Direct slicing of NumPy arrays often creates a view, not a copy. Modifying a view modifies the original buffer.

source = np.array([1, 2, 3])
view_slice = source[:]  # This is a view!
view_slice[0] = 99      # source becomes [99, 2, 3]

# Create an independent copy
independent = source.copy()
independent[0] = 5      # source remains unaffected

Memory Reference vs. Copies

  • Assignment: Assigns a reference (pointer). Variables point to the same object.
  • Shallow Copy: Creates a new container but inserts references to the original children. In Python lists, nested objects remain aliased. In NumPy, .copy() typically prdouces an independent copy of the underlying data buffer.
  • Deep Copy: Recursively duplicates all nested structures. Required for complex Python containers where complete independence from the original object graph is needed.

When working with structured data, always distinguish between aliasing (list logic) and buffering (NumPy logic). Avoid assuming list behavior applies to ndarray objects.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.