1. Introduction
A list is Python's most commonly used built-in data structure for storing an ordered collection of items. Lists are mutable, meaning their contents can be changed after creation, and they can hold items of different types in the same list.
Cricket analogy: A team's playing XI list, which can be edited between matches -- swapping an injured bowler for a new one -- and can mix specialist batters with all-rounders, is like a Python list: mutable and holding mixed types.
Lists are defined using square brackets [] with items separated by commas. Because they are ordered, every item has a fixed position (index) that can be used to access or modify it, and lists can grow or shrink dynamically as elements are added or removed.
Cricket analogy: A batting order written with square brackets like [Rohit, Gill, Kohli] fixes each player's position number, but the team sheet can grow when a reserve is added or shrink when a player is ruled out.
2. Syntax
# Creating lists
empty_list = []
numbers = [1, 2, 3, 4, 5]
mixed = [1, "two", 3.0, True]
# Accessing elements
first = numbers[0]
last = numbers[-1]
# Modifying elements
numbers[0] = 100
# Common operations
numbers.append(6)
numbers.insert(1, 99)
numbers.remove(99)
numbers.pop()
sliced = numbers[1:3]3. Explanation
Lists support indexing (starting at 0), negative indexing (from the end), and slicing to extract sub-lists. Methods like append(), insert(), remove(), pop(), sort(), and reverse() let you modify a list in place, while functions like len() and sorted() operate on lists without necessarily changing them.
Cricket analogy: batting_order[0] gets the opener, batting_order[-1] gets the last man in, and batting_order[3:6] slices the middle order; append() adds a new player, sort() ranks by average, while len() just counts the squad without changing it.
Because lists are mutable objects, assigning one list variable to another does not create a copy — both names refer to the same underlying object in memory. This is a frequent source of bugs for beginners.
Cricket analogy: Handing a teammate your exact scorecard clipboard rather than a photocopy means any edit they make -- crossing out a run -- shows on your copy too; assigning one list variable to another works the same way, sharing the same object.
Aliasing trap: a = b does NOT copy a list, it makes a and b point to the same list object. Calling a.append(x) will also change what b sees, because there is only one list in memory. To make an independent copy, use b = a.copy(), b = list(a), or b = a[:].
List slicing list[start:stop:step] never raises an IndexError even for out-of-range indices — it simply returns as much as is available (or an empty list). This makes slicing safer than direct indexing for exploratory code.
4. Example
# Demonstrating list mutability and aliasing
a = [1, 2, 3]
b = a # b is an alias of a, not a copy
b.append(4)
print("a:", a)
print("b:", b)
c = a.copy() # c is an independent copy
c.append(99)
print("a after copy modified:", a)
print("c:", c)
numbers = [5, 3, 8, 1]
numbers.sort()
print("sorted:", numbers)
print("reversed slice:", numbers[::-1])5. Output
a: [1, 2, 3, 4]
b: [1, 2, 3, 4]
a after copy modified: [1, 2, 3, 4]
c: [1, 2, 3, 4, 99]
sorted: [1, 3, 5, 8]
reversed slice: [8, 5, 3, 1]6. Key Takeaways
- Lists are ordered, mutable, and can store mixed data types.
- Assigning
a = bcreates an alias, not a copy; use.copy(),list(), or[:]for a real copy. - Negative indices count from the end; slicing
[start:stop:step]never raises IndexError. append(),insert(),remove(),pop()modify a list in place.sorted(list)returns a new sorted list, whilelist.sort()sorts in place and returnsNone.list[::-1]is a common idiom to reverse a list without mutating the original.
Practice what you learned
1. What is the output of `a = [1,2,3]; b = a; b.append(4); print(a)`?
2. Which method removes and returns the last item of a list?
3. What does `numbers[::-1]` return?
4. Which of these creates an independent copy of list `a`?
5. What is the result of `sorted([3,1,2])` versus `[3,1,2].sort()`?
6. What will `list('abc')` evaluate to?
Was this page helpful?
You May Also Like
Tuples in Python
An immutable, ordered sequence type in Python, covering tuple syntax, the nested-mutable-element trap, tuple packing/unpacking, and use cases.
List Comprehension in Python
A concise Python syntax for building lists from iterables in a single expression, including conditionals and the memory-difference vs generator expressions.
Dictionaries in Python
Python's key-value mapping type, covering hashable keys, insertion order guarantees (3.7+), common dict methods, and typical gotchas.
Sets in Python
An unordered collection of unique, hashable elements in Python, covering set creation, operations, and the no-order/no-duplicates gotcha.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics