A Python list is an ordered, mutable collection that holds items of any type inside square brackets []. Lists allow duplicate entries and preserve insertion order, making them the workhorse container in Python.
You access items by zero-based index, slice ranges cleanly, and grow or shrink a list dynamically at runtime.
Use append() to add one item to the end, extend() to add multiple items from another iterable, and pop() to remove and return an item.
tasks = ["lint", "test", "build"]
tasks.append("deploy")
tasks.insert(1, "typecheck")
done_task = tasks.pop()
print(tasks[:2]) # ['lint', 'typecheck']
print(done_task) # 'deploy'
List comprehensions build a new list in one readable expression instead of writing an empty list plus a for loop. Notice the difference between items.sort() (mutates in place, returns None) and sorted(items) (returns a new sorted list).
prices = [12, 45, 8, 30]
discounted = [round(p * 0.9, 2) for p in prices if p >= 10]
ordered = sorted(discounted, reverse=True)
print(ordered) # [40.5, 27.0, 10.8]
x = items.sort(): .sort() modifies items in place and returns None; use sorted(items) when you need a new list.copy = original: That creates an alias; copy with original.copy() or list(original).Yes. A single list can store strings, integers, and objects together, though keeping items uniform makes processing easier.
append() and extend()?append([4, 5]) adds one nested list item at the end, while extend([4, 5]) unpacks and appends 4 and 5 individually.
copy.deepcopy()?items.copy() works for flat lists of primitives. If your list contains nested lists or dicts, use copy.deepcopy(items) so inner edits stay isolated.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.