Lists as Arrays
A Python list stores items in order, each at a numbered position (its index, starting at 0). Because the computer can jump straight to any index, reading or changing items[i] takes the same time however long the list is: O(1).
Adding at the end with append is also fast. But inserting or deleting at the front or middle forces every later item to shift along one place, which is O(n). Searching for a value with in or .index() looks at items one by one, also O(n).
Slices such as items[1:4] copy a section into a new list.
Which list operation is the slowest for a long list?
Reverse the list in place without using reverse() or slicing, by swapping the first and last items, then the second and second-last, and so on. Print the list.
Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.
Tip: use the left and right arrow keys to move between lessons.