Skip to content

S1Q3 · Rearrange Even-Length Tuple by Placing Middle Elements at Ends

⚡ Quick Reference

Function: add_middle_elems_to_ends_in_even_length_tuple(t: tuple) -> tuple

Core idea: find the two middle elements, prepend the first and append the second, keeping the rest in original order.

def add_middle_elems_to_ends_in_even_length_tuple(t: tuple) -> tuple:
    mid   = len(t) // 2
    mid1, mid2 = t[mid - 1], t[mid]
    return (mid1,) + t[:mid - 1] + t[mid + 1:] + (mid2,)

Key rules: - For even-length tuple of length n: two middle elements are at indices n//2 - 1 and n//2 - mid1 goes to position 0, mid2 goes to the last position - The remaining elements keep their original relative order


Problem Statement

Problem

Write a function that extracts the two middle elements of an even-length tuple, places the first at the beginning and the second at the end, and returns the rearranged tuple.

Examples:

Input
(10, 20, 30, 40)
Output
(20, 10, 40, 30)
Input
("Lenovo", "Dell", "HP", "Asus")
Output
("Dell", "Lenovo", "Asus", "HP")

Understanding the structure

For t = (10, 20, 30, 40), len = 4, mid = 2:

Index:     0    1    2    3
Value:    10   20   30   40
               ↑    ↑
              mid1 mid2  (indices mid-1=1 and mid=2)

Result structure:
(mid1,) + t[:mid-1]  + t[mid+1:]  + (mid2,)
  (20,) +   t[:1]    +   t[3:]    +  (30,)
  (20,) +   (10,)    +   (40,)    +  (30,)
= (20, 10, 40, 30) ✓

For t = ("Lenovo", "Dell", "HP", "Asus"), mid = 2:

mid1 = "Dell", mid2 = "HP"
t[:1] = ("Lenovo",), t[3:] = ("Asus",)
→ ("Dell", "Lenovo", "Asus", "HP") ✓

Tracing both examples

t mid mid1 mid2 t[:mid-1] t[mid+1:] Result
Ex 1 (10,20,30,40) 2 20 30 (10,) (40,) (20,10,40,30)
Ex 2 ("Lenovo","Dell","HP","Asus") 2 "Dell" "HP" ("Lenovo",) ("Asus",) ("Dell","Lenovo","Asus","HP")

Solution approaches

def add_middle_elems_to_ends_in_even_length_tuple(t: tuple) -> tuple:
    mid  = len(t) // 2
    mid1 = t[mid - 1]
    mid2 = t[mid]
    return (mid1,) + t[:mid - 1] + t[mid + 1:] + (mid2,)
def add_middle_elems_to_ends_in_even_length_tuple(t: tuple) -> tuple:
    n    = len(t)
    mid  = n // 2

    mid1 = t[mid - 1]   # first middle element
    mid2 = t[mid]        # second middle element

    left  = t[:mid - 1]  # elements before mid1
    right = t[mid + 1:]  # elements after mid2

    return (mid1,) + left + right + (mid2,)
def add_middle_elems_to_ends_in_even_length_tuple(t: tuple) -> tuple:
    mid  = len(t) // 2
    lst  = list(t)
    mid1 = lst.pop(mid - 1)     # removes and returns first middle
    mid2 = lst.pop(mid - 1)     # after pop, mid is now at mid-1 again
    return (mid1,) + tuple(lst) + (mid2,)

After removing mid1 with pop(mid-1), the second middle element shifts to index mid-1. Two pops extract both middles cleanly; the remaining list becomes the inner part.

add_middle_elems_to_ends_in_even_length_tuple = lambda t: (
    (lambda m: (t[m-1],) + t[:m-1] + t[m+1:] + (t[m],))(len(t)//2)
)

Key takeaways

01

Middle indices for even-length tuple: n//2-1 and n//2

For a tuple of length 4: indices 1 and 2. For length 6: indices 2 and 3. The formula mid = len(t)//2 gives the second middle; mid-1 gives the first.

02

Single-element tuples need a trailing comma

(mid1,) - the comma makes it a tuple. Without it, (mid1) is just parentheses around a value, not a tuple. Concatenating a non-tuple with a tuple raises a TypeError.

03

pop() shifts indices - account for it

After lst.pop(mid-1) removes the first middle element, the list is one shorter. The second middle element is now at index mid-1 (not mid). The second pop(mid-1) correctly grabs it.