🌱BeginnerData Structures
Lists
Python's most versatile collection: creating, indexing, slicing, and modifying ordered lists.
Overview
A list is Python's most commonly used collection type. It stores an ordered sequence of items that can be of any type integers, strings, other lists, or even a mix of types. Lists are mutable, meaning you can add, remove, and change elements after creation.
Lists are defined with square brackets: [1, 2, 3] or []. You access elements by zero-based index (list[0] is the first element), and negative indices count from the end (list[-1] is the last). Slicing extracts sub-lists: list[start:stop:step].
The list type has numerous built-in methods: append(), extend(), insert(), remove(), pop(), sort(), reverse(), index(), count(), and copy(). Understanding which operation you need (and its time complexity) is an important part of writing efficient Python.
Key Points
- •Lists are ordered, mutable, and allow duplicate values
- •Create with [] or list(): empty = [], numbers = [1, 2, 3], mixed = [1, "a", True]
- •Access by index (0-based): lst[0] first, lst[-1] last; slice: lst[1:4] sublist
- •append(x) adds to end; insert(i, x) inserts at index i; extend(iterable) adds all items from another sequence
- •remove(x) deletes the first occurrence of x; pop(i) removes and returns item at index i (default last)
- •len(lst) gives count; sorted(lst) returns a new sorted list; lst.sort() sorts in place
- •in operator: `3 in [1, 2, 3]` → True
Examples
Creating and accessing lists
fruits = ["apple", "banana", "cherry", "date"]
print(fruits[0]) # apple
print(fruits[-1]) # date
print(fruits[1:3]) # ['banana', 'cherry']
print(len(fruits)) # 4
# Modifying
fruits[1] = "blueberry"
print(fruits) # ['apple', 'blueberry', 'cherry', 'date']Adding and removing items
nums = [3, 1, 4, 1, 5]
nums.append(9) # add to end → [3, 1, 4, 1, 5, 9]
nums.insert(0, 0) # insert at index 0 → [0, 3, 1, 4, 1, 5, 9]
nums.remove(1) # remove first 1 → [0, 3, 4, 1, 5, 9]
last = nums.pop() # removes and returns 9 → nums=[0, 3, 4, 1, 5]
print(nums, last)Sorting and searching
names = ["Charlie", "Alice", "Bob", "Dave"]
# sorted() returns new list, original unchanged
print(sorted(names)) # ['Alice', 'Bob', 'Charlie', 'Dave']
print(names) # original unchanged
# .sort() in-place
names.sort()
print(names) # ['Alice', 'Bob', 'Charlie', 'Dave']
names.sort(reverse=True)
print(names) # ['Dave', 'Charlie', 'Bob', 'Alice']
print("Bob" in names) # True
print(names.index("Bob")) # 2Common Mistakes
- IndexError by going out of bounds: lst[5] on a 5-element list valid indices are 0–4
- Confusing .sort() (in-place, returns None) with sorted() (returns new list)
- Copying by assignment: `b = a` makes b point to the same list. Use `b = a[:]` or `b = a.copy()` for an independent copy
Note
Use `lst[-1]` to access the last element instead of `lst[len(lst)-1]` it's shorter and more Pythonic. The same goes for slices: `lst[-3:]` gives the last three elements.
Exercises
Write Python code to solve each exercise. Run it in your browser and check your output.
Loading editor…
Completed: 0 / 5 exercises
Knowledge Check
Question 1 of 4