Programming Fundamentals › Collections
List
An ordered collection; a dynamic array or a linked list depending on the language.
Also known as: lists, dynamic array, ArrayList
A list is an ordered collection that can grow and shrink. In Python it’s the everyday collection type. In other languages it may be called an array, vector or ArrayList, or be a separate linked structure.
tasks = ["write", "test"]
tasks.append("deploy") # add at the end
tasks.insert(0, "plan") # add at the front
tasks.remove("test")
tasks.pop() # remove and return the last
len(tasks)
tasks.sort()
sorted(tasks, reverse=True) # returns a new list
List or something else?
| Need | Better choice |
|---|---|
| Ordered items that change | list |
| A fixed collection that shouldn’t change | tuple |
| Fast “is this in there?” and no duplicates | set |
| Lookup by key | dictionary |
| Fast add and remove at both ends | a deque (collections.deque) |
Behaviors to know
- Mutable. Changing a list changes it for everyone holding a reference (mutable vs immutable).
b = adoesn’t copy. Usea.copy()orlist(a), and a deep copy for nested lists (shallow vs deep copy).inis slow on big lists. It scans every element. Use a set for repeated lookups.- Inserting or removing near the start is slow for array-backed lists.
sort()changes the list and returnsNone, a classic Python mistake.- Building with a comprehension is idiomatic:
[x * 2 for x in nums if x > 0].