liveBook · Manning

List

As explained in Grokking Data Structures

A list is a Python sequence type that stores an ordered collection of elements and supports dynamic resizing and heterogeneous element types.

A Python list is an ordered, sequence-like collection that can hold numbers, strings, and other lists together. It resizes dynamically as elements are added, making it more flexible than a fixed-size array, though that flexibility can reduce performance in critical code. section 2.7

At the abstract-data-type level, a list is a sequence whose elements can be traversed and accessed sequentially. Arrays and linked lists are two different data-structure refinements of that same idea. section 7.3

Choose a list or array-like structure according to the flexibility and performance your program needs:

  • Use a Python list when convenient dynamic resizing and mixed element types matter more than maximum performance. section 2.7
  • Consider a type-consistent array implementation when the elements share a type; array.array remains dynamic, while NumPy can provide fixed-size arrays. section 2.7
  • Remember that arrays and linked lists support the list abstraction with different operation costs, so performance-sensitive code deserves measurement before optimization. section 2.7 section 7.3

A list-backed heap

A heap can store its elements in a Python list. The list acts as the internal array while helper methods calculate parent and child positions and compare element priorities. section 10.16

class Heap:
    def __init__(self, elements=None, element_priority=lambda x: x):
        self._priority = element_priority
        if elements is not None and len(elements) > 0:
            self._heapify(elements)
        else:
            self._elements = []

This implementation shows a Heap class using a Python list internally and rebuilding its structure from existing elements. section 10.16

How Grokking Data Structures covers it

How deep it goes
Defines the list abstraction and compares implementations, but defers detailed Python list operations and implementation internals.
Examples
Demonstrates lists through array comparisons, a custom array implementation, and a heap whose elements occupy a Python list.
What it assumes
Assumes basic Python familiarity while teaching the distinction between abstract data types and concrete data structures.
What you can do after
Evaluate whether Python lists, typed arrays, or linked lists fit a container’s flexibility and performance requirements.
How it connects
Connects lists with dynamic arrays, static arrays, linked lists, abstract data types, heaps, and priority queues.

Also explained in

Compare how 21 books explain list →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Data Structures. The section links point to the parts of the book it draws on.