Imagine we want to board (or insert) a passenger from the rear door of the plane. To insert items into the rear of the deque, we need to implement an add_last(item) method. This method should:

  • Take in one input parameter, item, which is the element to add to the rear of the deque.
  • Since the rear of our deque is the leftmost side, item should become the first item in our list, which means we need to prepend it, or insert it at position 0.

The time complexity of this method is O(1), since adding item to the elements list only requires iteration over one element to execute the method.



Implement the add_last() method based on the above implementation details in Python.


Test calling the add_last() method by adding the number 42 to the deque rear.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?