Why doesn't Python have a linked list?

September 22, 2020

I recently solved a LeetCode problem with linked lists, which Python doesn't have, so I did some research into:

  1. What Python does have
  2. Whether a linked list is faster than Python's options
  3. When you would need a linked list in Python

No linked lists? Then what does Python have?

Python has native lists that are stored as C arrays, so they have O(n)O(n) inserts and O(1)O(1) random access.

Python also has a double-ended queue, the deque (pronounced "deck").

How do deques work?

Data for deque objects is stored in a doubly-linked list of fixed
length blocks. This assures that appends or pops never move any
other data elements besides the one being appended or popped. Python 3.8 source

This means that deques have O(1)O(1) appends and pops from either end.

Deques have also had an insert function since Python 3.5. Inserts are O(n)O(n) since they're implemented with the rotate function.

Is a custom linked list faster than Python's deque?

We're going to test Python's deque vs. a custom linked list with:

  1. Inserts at the beginning.
  2. Inserts at the middle.

We'll use this minimal linked list implemenetation for testing.

Inserts at the beginning:

For inserts at index 00, the deque is twice as fast in practice:

dequeLinkedList
deque vs LinkedList runtime for inserts at index zero
deque vs LinkedList runtime for inserts at index zero

Inserts at the middle:

If we do a fixed number of inserts to the middle, the deque is 100x faster!

sizedequeLinkedList
1000.000040.00126
1,0000.000100.00428
10,0000.000430.04306
100,0000.004500.42688
1,000,0000.045254.34151

Custom linked lists can be faster!

Custom linked lists can be faster than deques when you're doing inserts to the middle of the structure. If you already have a reference to a Node, you can insert to the linked list at that location in O(1)O(1) time.

Deques are built on "fixed length blocks" of multipe elements. An insert to the middle of the structure could overflow a block and force an O(n)O(n) shift of elements.

When would you need to use a linked list?

A true linked list would only be useful in an algorithm that involves holding onto references to Nodes and manipulating the list with those.

It would require middle-inserts to be a big part of the algorithm in order to overcome the practical speed benefits we saw for the deque.

You might use a linked list to implement other data structures, but the Python language has other solutions for that already:

Python stacks, queues, and heaps are all sets of functions that use native lists as the underlying data structure.
The synchronized Queue class is built over a deque.

tl;dr; You rarely need a true linked list in Python and most of the time a deque is faster.