Lists, Tuples, and Slicing
Master Python list operations, slicing syntax, and tuple immutability with hands-on examples drawn from classic coding challenges.
Python Lists: Dynamic Arrays
A Python list is a dynamic array that holds anything and grows on its own. It is ordered, changeable, and gives you instant O(1) access by index. The code shows the basics.
nums = [3, 1, 4, 1, 5]
print(nums[0]) # 3
print(nums[-1]) # 5 (last element)
nums.append(9)
print(len(nums)) # 6Common List Operations
Know these by heart: append and pop at the end are O(1), but insert at the front is O(n). Avoid remove in tight loops — it rescans every time.
stack = []
stack.append(1)
stack.append(2)
stack.append(3)
print(stack.pop()) # 3 O(1)
print(stack) # [1, 2]
# insert at index 0 is O(n)
stack.insert(0, 0)
print(stack) # [0, 1, 2]All lessons in this course
- Lists, Tuples, and Slicing
- Dictionaries and Sets in Python
- Comprehensions and Built-ins
- Functions, Closures, and Lambda