Ordered, Flexible Collections in Python¶

Lists are Python's everyday tool for ordered data¶
Once you know you need a sequence, a list is usually the first structure to reach for. Lists preserve order, let you update values, and work well for everything from monthly sales figures to ordered workflow steps and transaction histories.
Why lists matter¶
Lists are useful when:
order matters
values may change over time
you need to append, remove, or iterate through items
the same type of record appears many times in a sequence
Continuity from the data structures overview
The chapter overview asked whether order matters. Lists are the first concrete answer to that question: they are ideal when position and sequence are part of the meaning of the data.
products = ["Notebook", "Pen", "Marker"]
print(products[0])
products.append("Stapler")
print(products)revenues = [12000, 13500, 12800]
print(sum(revenues))
for value in revenues:
print(value)Worked Example: tracking business values in order¶
The two code cells above already show the two most common beginner uses of lists:
storing named items in order
storing numeric values that can be summarized and iterated over
When you read those examples, notice three ideas:
indexing lets you reach a specific position such as the first product
methods like
append()change the list in placeloops make it easy to process every value in sequence
Exercises¶
Create a list of three customer names and print the second one.
Add a new item to a product list.
Sum a list of quarterly profits.
Write one sentence explaining why a list is a better fit than a single variable for monthly revenue values.
Hint
If the data has multiple items in order, a list usually expresses the idea more clearly than separate standalone variables.
Quick Summary
lists keep values in order
lists are mutable, which means they can be changed
lists support indexing, iteration, and many useful methods
lists are often the first choice for ordered real-world data
8. Interactive Code¶
Expected output
['Notebook', 'Pen', 'Marker', 'Stapler']Expected output
13500
3List Methods¶
Each example below focuses on one list method so learners can test it in isolation.
append()¶
Use append() to add one item to the end of a list.
extend()¶
Use extend() to add multiple items from another iterable.
insert()¶
Use insert() when an item must go to a specific position.
remove()¶
Use remove() to delete the first matching value.
pop()¶
Use pop() to remove and return an item, usually from the end.
count()¶
Use count() to find how many times a value appears.
index()¶
Use index() to locate the position of the first matching value.
sort()¶
Use sort() to order list values in place.
reverse()¶
Use reverse() to flip the current order of the list.
copy()¶
Use copy() when you want a separate list before making changes.
9. Guided Practice¶
Which method adds an element to the end of a list?¶
What does `len(revenues)` return in the example above?¶
Deeper Python List Patterns¶
Adapted for this course from the Python Tutorial data structures chapter.
This section extends the beginner examples into patterns you will use repeatedly:
method-based updates
list comprehensions for compact transformations
nested comprehensions for structured reshaping
delfor removing items or slices
How to read this section
Do not try to memorize every method at once. Focus on the pattern each example demonstrates and the kind of problem it helps solve.
# More on list methods
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
print(fruits.count('apple'))
print(fruits.index('banana'))
print(fruits.index('banana', 4))
fruits.reverse()
print('Reversed:', fruits)
fruits.append('grape')
fruits.sort()
print('Sorted + appended:', fruits)
fruits.pop()
print('After pop:', fruits)
# insert, extend, remove, copy, clear
numbers = [1, 2, 4]
numbers.insert(2, 3)
numbers.extend([5, 6])
numbers.remove(6)
numbers_copy = numbers.copy()
print('Numbers:', numbers)
print('Copy:', numbers_copy)
temp = ['a', 'b']
temp.clear()
print('Cleared list:', temp)# List comprehensions
squares = [x**2 for x in range(10)]
print('Squares:', squares)
pairs = [(x, y) for x in [1, 2, 3] for y in [3, 1, 4] if x != y]
print('Pairs:', pairs)
vec = [-4, -2, 0, 2, 4]
print('Absolute values:', [abs(x) for x in vec])
print('Filtered positives:', [x for x in vec if x >= 0])
# Nested list comprehension: matrix transpose
matrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
]
transpose = [[row[i] for row in matrix] for i in range(4)]
print('Transpose:', transpose)
# del statement
a = [-1, 1, 66.25, 333, 333, 1234.5]
del a[0]
del a[2:4]
print('After deleting index and slice:', a)
del a[:]
print('After deleting all items:', a)