liveBook · Manning

Array

As explained in Python Workout, Second Edition

An array is a data structure whose elements occupy a fixed-size, homogeneous sequence of storage positions.

An array stores elements in fixed-size, homogeneous positions. Python lists are the usual array-like choice, but they are variable-length and may hold different object types. section 4.5

Internally, a Python list is an array of pointers to Python objects. Python reserves spare capacity, reallocating and moving those pointers when the reserved space runs out, so appending can incur hidden overhead. section 4.5

Observe list capacity

Run the supplied measurement loop with sys.getsizeof. The reported size stays constant across several appends, then jumps as Python allocates a larger backing array.

Practice with array-like data

Write plus_minus([10, 20, 30, 40, 50, 60]) to compute 10+20-30+40-50+60, producing 50; then implement myzip for equal-length iterables. section 4.5

How Python Workout, Second Edition covers it

How deep it goes
Explains fixed size, homogeneity, pointer storage, spare capacity, reallocation, and measurement, but stops short of formal complexity analysis.
Examples
Uses alternating-sum and zip exercises plus sys.getsizeof measurements of list growth.
What it assumes
Assumes familiarity with Python lists, tuples, iterables, and function-writing exercises.
What you can do after
Evaluate how Python lists behave as array-like structures and practice processing list or tuple data.
How it connects
Connects arrays with Python lists, tuples, Python objects, pointers, iterables, and zip.

Also explained in

Compare how 40 books explain array →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout, Second Edition. The section links point to the parts of the book it draws on.