Python

Lists

A structured, from-first-principles guide to Python lists — creation, indexing/slicing, modification, methods, comprehensions, copying and mutability, sorting/searching, performance, common algorithms, and real-world DevOps inventory scripts.

Python learning path

Explore Lists

← Python Fundamentals
Beginner

10.1 Introduction to Lists

What a list is, its core characteristics, why it's Python's general-purpose array, and how it's actually stored in memory as a resizable, over-allocated array of references.

Read guide
Beginner

10.2 Creating Lists

Empty lists, list literals, the list() constructor, splitting a string into characters, materializing a range(), and building nested or mixed-type lists.

Read guide
Beginner

10.3 List Indexing and Slicing

Positive and negative indexing, chaining indices into nested lists, IndexError, and slicing with l[start:stop:step] -- including reversal, copying, and slice assignment.

Read guide
Beginner

10.4 Modifying Lists

Updating an element by index, growing a list with append() and extend(), inserting at a position, and replacing a whole range at once with slice assignment -- all of which mutate the list in place.

Read guide
Beginner

10.5 Removing Elements

The four ways to remove items from a list -- remove() by value, pop() by index (which returns what it removes), clear() to empty it, and the del statement, including deleting an entire slice at once.

Read guide
Beginner

10.6 List Operators

Concatenation and repetition build brand-new lists, membership testing is a linear scan, and comparison works element-by-element and lexicographically -- the same rule used for tuple comparison.

Read guide
Beginner

10.7 List Methods

A consolidated reference for every built-in list method, grouped by what it does -- add (append, extend, insert), remove (remove, pop, clear), query (index, count), and reorder/copy (sort, reverse, copy).

Read guide
Beginner

10.8 Built-in Functions

Global functions that accept a list as an argument -- len(), max(), min(), sum(), sorted(), any(), and all() -- as distinct from methods called on the list itself.

Read guide
Beginner

10.9 Traversing and List Comprehensions

Four idiomatic ways to visit every element of a list -- for, while, enumerate(), and zip() -- plus list comprehensions as the compact, expression-based alternative to a manual loop with append().

Read guide
Intermediate

10.10 Nested Lists and Matrices

Creating, accessing, and updating nested lists using chained indices, iterating a 2D matrix row by row, and why [[0]*n]*n builds a matrix of shared, not independent, rows.

Read guide
Intermediate

10.11 Copying and Mutability

Why b = a shares one list instead of copying it, the list-specific shallow-copy shortcuts (copy() and l[:]), how mutation is visible through every shared reference including function arguments, and the mutable-default-argument trap.

Read guide
Intermediate

10.12 Sorting and Searching

sort() vs sorted() -- in-place versus a new list -- plus linear search (works on any list, O(n)) and binary search (requires a sorted list, O(log n)).

Read guide
Intermediate

10.13 Performance

The time complexity of every core list operation, why a list uses somewhat more memory than an equivalent tuple, and why append() is O(1) amortized while insert(0, x) is O(n).

Read guide
Intermediate

10.14 Common Algorithms

Classic list algorithms built from tools covered earlier in this chapter -- reverse, order-preserving deduplication with dict.fromkeys(), find max/min, merging two lists, and splitting one into fixed-size chunks.

Read guide
Intermediate

10.15 Lists in DevOps

Lists as the natural in-memory shape of file and log contents, plus real infrastructure patterns -- server inventories, filtering Amazon EC2 instances, Docker container checks, Kubernetes pod filtering, and log-severity filtering.

Read guide
Intermediate

10.16 Common Mistakes

The most common list bugs in Python -- off-by-one IndexError, silently skipping elements by mutating a list while iterating over it, and the shared-reference and mutable-default-argument traps.

Read guide
Beginner

10.17 Best Practices

Preferring a list comprehension over a manual loop when it stays readable, choosing append() over insert(0, ...), converting to a set for repeated membership checks, and picking the right collection type for the job.

Read guide
Intermediate

10.18 Interview Questions

Frequently asked and scenario-based Python list interview questions covering list vs tuple, append() vs insert(), sort() vs sorted(), shallow vs deep copy, and classic coding problems like safe iteration and binary search.

Read guide
Intermediate

10.19 Hands-on Exercises

Practice programs reinforcing Python list concepts -- an inventory manager class, a todo app, a log analyzer, a CSV processor, and mini projects grouping Amazon EC2 instances and tracking missing Docker containers.

Read guide
Beginner

Lists: Chapter Practice

Apply lists with a mini lab and knowledge check.

Read guide