Start learning free

LESSON 121 OF 673 · LISTS - CHALLENGES

Move to the Front in Python: pop and insert

Practice Read the full explanation

You get a to-do list items, a list of strings, and a number index. Return the list with the item at index moved to the front.

Take the item out of its place and put it at index 0. The other items keep their order.

 

Example 1:

Input: items = ["wash", "shop", "cook", "read"], index = 2
Output: ["cook", "wash", "shop", "read"]
Explanation: The item at index 2 is "cook". It moves to the front.
Then "wash" and "shop" move one place to the right.

Example 2:

Input: items = ["tea", "coffee"], index = 0
Output: ["tea", "coffee"]
Explanation: The item at index 0 is already at the front.
Nothing changes.

Example 3:

Input: items = ["call", "pay", "call", "walk"], index = 2
Output: ["call", "call", "pay", "walk"]
Explanation: The "call" at index 2 moves to the front.
The other "call" and "pay" move one place to the right.

 

Constraints:

  • items has 1 to 10 words.
  • index is from 0 to the length of items minus 1.
  • Every word is made of lowercase English letters.
  • The same word may appear more than once.
STEP 1 OF 3
    Ctrl + Enter to check

    PYTHON · LISTS - CHALLENGES

    Move To Front in Python, explained

    Problem overview

    The method moveToFront gets a to-do list items (a list of strings) and a number index, and returns the list with the item at that index moved to the front. The item is taken out of its place and put at index 0, and the other items keep their order. The index is always valid, and the same word may appear more than once.

    Examples:

    • ["wash", "shop", "cook", "read"] with index = 2 gives ["cook", "wash", "shop", "read"].
    • ["tea", "coffee"] with index = 0 gives ["tea", "coffee"], because the item is already first.
    • ["call", "pay", "call", "walk"] with index = 2 gives ["call", "call", "pay", "walk"]. The "call" at index 2 moves, not the one at index 0.

    Approach: take it out, then put it in front

    Two list methods do the whole job. items.pop(index) removes the item at that position and returns it, and the items behind it close the gap. items.insert(0, item) then puts it at index 0, and everything else moves one place to the right. The items that were before the moved one are pushed right by the insert, and the ones after it were never disturbed, so their relative order holds.

    The order of the two calls matters. The index refers to the original list, so the item must be read and removed first. If you inserted first, the list would be one longer and every position before the old one would shift.

    Step by step

    1. Remove the item at index with pop and keep what it returns in item.
    2. Insert item at index 0 with insert(0, item).
    3. Return items, which has been changed in place.

    The solution in Python

    class Solution:
        def moveToFront(self, items: List[str], index: int) -> List[str]:
            item = items.pop(index)
            items.insert(0, item)
            return items
    

    On the three examples it prints:

    ['cook', 'wash', 'shop', 'read']
    ['tea', 'coffee']
    ['call', 'call', 'pay', 'walk']
    

    The method changes the list it receives and returns that same list. Anyone holding the original variable sees the move too. If you need the old order kept, work on a copy first, with items = items[:] or list(items). The lesson's tests only look at what the method returns.

    Another way: build a new list

    You can also leave the input alone and build the answer from three pieces: the chosen item, everything before it, and everything after it.

    def move_to_front(items, index):
        return [items[index]] + items[:index] + items[index + 1:]
    

    For the third example this gives ['call', 'call', 'pay', 'walk'], and the list passed in is still ['call', 'pay', 'call', 'walk'] afterwards. The slice items[:index] holds the items before the chosen one, and items[index + 1:] holds the ones after it. Both versions return the same list for the same input; they differ in whether the argument is changed.

    Edge cases and common mistakes

    • Removing by value. items.remove(items[index]) deletes the first equal item, not the one at index. For the third example it removes the "call" at index 0, so the list comes out unchanged: ['call', 'pay', 'call', 'walk'].
    • Inserting first and then popping the same index. After items.insert(0, items[2]) the list is longer, and items.pop(2) removes the wrong item. For the first example it leaves ['cook', 'wash', 'cook', 'read'].
    • Index 0. It needs no special case: the item is popped and inserted at the same place.
    • A one-item list. The pop leaves an empty list and the insert puts the item back, so it works as well.
    • Using append by habit. append adds at the end. To put an item at the front you need insert(0, item).

    Testing

    Try moving the last item (index = len(items) - 1), which should come out first with the rest shifted right, and a one-item list, which should come back unchanged.

    Keep going with Python500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free