100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

Lists in Python

A mutable, ordered sequence type in Python used to store collections of items, with a deep dive into mutation, aliasing, and common list operations.

Data StructuresBeginner12 min readJul 7, 2026
Analogies

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

python
# 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

python
# 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

text
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 = b creates 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, while list.sort() sorts in place and returns None.
  • list[::-1] is a common idiom to reverse a list without mutating the original.

Practice what you learned

Was this page helpful?

Topics covered

#Python#PythonProgrammingStudyNotes#Programming#ListsInPython#Lists#Syntax#Explanation#Example#DataStructures#StudyNotes#SkillVeris