Learn

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

  • Take in one input parameter, item, which is the element to add to the front of the deque.
  • Append the item to the elements list. Since the front of our deque is the rightmost side, item should become the last item in our list, which means we need to append.

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

Instructions

1.

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

2.

Test calling the add_first() method by adding the number 5 to the deque front.

3.

Test calling the add_first() method by adding the number 20 to the deque front.

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?