- Courses
- 💻Programming Languages
- 🐍Python
- Core Data Structures
- Lists
On this page
Lists
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
What a List Is
A list is Python’s primary ordered, mutable data structure. It stores a sequence of items in a single variable, allowing you to group related values together. Lists can contain numbers, strings, Booleans, other lists, or any Python object.
Lists are written using square brackets:
numbers = [10, 20, 30]
words = ["apple", "banana", "cherry"]
mixed = [1, "two", True]
Lists preserve order, allow duplicates, and can grow or shrink dynamically.
Creating Lists
You can create lists in several ways:
empty = []
values = [1, 2, 3]
letters = list("abc") # ['a', 'b', 'c']
The list() constructor converts iterables into lists.
Accessing List Elements (Indexing)
Each item in a list has an index, starting at 0.
items = ["a", "b", "c"]
print(items[0]) # 'a'
print(items[1]) # 'b'
Negative indices access items from the end:
print(items[-1]) # 'c'
print(items[-2]) # 'b'
Indexing is essential for retrieving specific elements.
Slicing Lists
A slice extracts a portion of a list.
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4]) # [20, 30, 40]
print(numbers[:3]) # [10, 20, 30]
print(numbers[3:]) # [40, 50]
Slicing never raises an error when the range exceeds the list length.
Modifying Lists (Mutability)
Lists are mutable, meaning you can change their contents.
Changing an Item
values = [1, 2, 3]
values[1] = 20
Adding Items
values.append(4)
values.insert(1, 15)
Removing Items
values.remove(20)
last = values.pop() # removes and returns last item
Mutability makes lists ideal for dynamic data.
Looping Through Lists
A for loop processes each element in order.
fruits = ["apple", "banana", "cherry"]
for f in fruits:
print(f)
This is the most common way to work with list data.
Checking Membership
Use the in operator to test whether a value appears in a list.
print("apple" in fruits) # True
print("orange" in fruits) # False
Membership checks are essential for validation and filtering.
Useful List Functions and Methods
Python provides several built‑in tools for list manipulation.
len(values) # number of items
min(values) # smallest value
max(values) # largest value
sorted(values) # returns a new sorted list
These operations are fundamental for data processing.
Nested Lists
A list can contain other lists, forming multi‑dimensional structures.
matrix = [
[1, 2],
[3, 4]
]
Access nested elements using multiple indices:
print(matrix[1][0]) # 3
Nested lists are useful for representing grids, tables, and structured data.
Why Lists Matter
Lists are one of the most important data structures in Python. They allow you to:
- Store collections of related data
- Process items efficiently with loops
- Modify data dynamically
- Build more complex structures such as matrices, queues, and buffers
Mastering lists is essential before learning tuples, dictionaries, sets, and advanced data‑handling techniques in later lessons.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Core Data Structures
Next
Tuples
-
What a List Is
-
Creating Lists
-
Accessing List Elements (Indexing)
-
Slicing Lists
-
Modifying Lists (Mutability)
- Changing an Item
- Adding Items
- Removing Items
-
Looping Through Lists
-
Checking Membership
-
Useful List Functions and Methods
-
Nested Lists
-
Why Lists Matter
