Skip to content

S1Q3 · Rotate Even Indices

⚡ Quick Reference

Function: rotate_even(l: list, k: int) -> list

Core idea: extract even-index elements, rotate them right by k, assign back.

def rotate_even(l: list, k: int) -> list:
    evens = l[::2]
    n = len(evens)
    rotated = evens[-k % n:] + evens[:-k % n]
    l[::2] = rotated
    return l

Key rules: - Even indices: 0, 2, 4, … → l[::2] - Rotate right by k: last k elements move to the front - l[::2] = rotated assigns back in-place using slice update - -k % n handles k larger than the number of even elements


Problem Statement

Problem

Write a function rotate_even(l, k) that rotates the elements at the even indices of l by k positions (to the right), leaving odd-index elements unchanged.

Examples:

Input
l=[1,2,3,4,5,6], k=1
Output
[5, 2, 1, 4, 3, 6]
Input
l=[1,2,3,4,5,6,7,8], k=3
Output
[3, 2, 5, 4, 7, 6, 1, 8]

Understanding the problem

Even-index elements (0-indexed) of [1,2,3,4,5,6] are at positions 0, 2, 4:

Index:  0  1  2  3  4  5
Value:  1  2  3  4  5  6
        ↑     ↑     ↑       ← even indices
       [1,    3,    5]

Rotating right by 1: the last element moves to the front:

[1, 3, 5]  →  [5, 1, 3]

Assign back to even positions:

Index 0 ← 5,  Index 2 ← 1,  Index 4 ← 3
Result: [5, 2, 1, 4, 3, 6] 

Right rotation by k using slicing

To rotate a list lst right by k: lst[-k:] + lst[:-k]

For [1, 3, 5] with k=1: [-1:] + [:-1] = [5] + [1, 3] = [5, 1, 3]

Use -k % n instead of k to safely handle cases where k >= len(lst).


Tracing both examples

Example 1: l=[1,2,3,4,5,6], k=1

evens = [1, 3, 5],  n = 3,  k % n = 1
rotated = evens[-1:] + evens[:-1] = [5] + [1, 3] = [5, 1, 3]
l[::2] = [5, 1, 3]
Result: [5, 2, 1, 4, 3, 6] 

Example 2: l=[1,2,3,4,5,6,7,8], k=3

evens = [1, 3, 5, 7],  n = 4,  k % n = 3
rotated = evens[-3:] + evens[:-3] = [3,5,7] + [1] = [3, 5, 7, 1]
l[::2] = [3, 5, 7, 1]
Result: [3, 2, 5, 4, 7, 6, 1, 8] 


Solution approaches

def rotate_even(l: list, k: int) -> list:
    evens = l[::2]
    n = len(evens)
    k = k % n                          # handle k >= n
    l[::2] = evens[-k:] + evens[:-k]
    return l

l[::2] = ... is a slice assignment -updates only the even-index positions in one step.

from collections import deque

def rotate_even(l: list, k: int) -> list:
    evens = deque(l[::2])
    evens.rotate(k)                    # deque.rotate rotates right by k
    l[::2] = list(evens)
    return l

collections.deque.rotate(k) rotates right by k natively -no slicing arithmetic needed. Very readable.

def rotate_even(l: list, k: int) -> list:
    even_indices = list(range(0, len(l), 2))
    evens = [l[i] for i in even_indices]
    n = len(evens)
    k = k % n
    rotated = evens[-k:] + evens[:-k]
    for i, idx in enumerate(even_indices):
        l[idx] = rotated[i]
    return l

Extract → rotate → assign back explicitly. Every step visible.

from collections import deque

def rotate_even(l: list, k: int) -> list:
    rotate = lambda lst, k: list((d := deque(lst), d.rotate(k), d)[2])
    l[::2] = rotate(l[::2], k)
    return l

Lambda uses walrus operator to create a deque, rotate it, and return the result in one expression.


Key takeaways

01

l[::2] = new_values -slice assignment

Assigning to a step-slice updates only those positions. l[::2] = rotated replaces even-index values without touching odd-index values -the hint in the problem points to exactly this.

02

Right rotation: lst[-k:] + lst[:-k]

Moving the last k elements to the front rotates right. Always reduce k with % n first to handle k >= n safely.

03

deque.rotate(k) for clean right rotation

collections.deque has a built-in rotate(k) method -positive k rotates right, negative rotates left. No slicing arithmetic needed at all.