S1Q2 · Join Even Index Strings¶
⚡ Quick Reference
Function: join_even_index_strings(words: list) -> str
Core idea: Concatenate the elements at index 0 and index 2.
Key rules: - Input list always has exactly four strings - Even indices are 0 and 2 only - Join means concatenation, no separator
Problem Statement¶
Problem
Write a function join_even_index_strings(words) that takes a list of exactly four strings and returns a new string formed by joining the strings at even indices (index 0 and index 2).
Examples:
join_even_index_strings(["apple", "banana", "cherry", "mango"])
'applecherry'
join_even_index_strings(["red", "green", "blue", "yellow"])
'redblue'
Understanding the problem¶
A four-element list has indices 0, 1, 2, 3. The even ones are 0 and 2.
| Index | ["apple", "banana", "cherry", "mango"] |
Even? |
|---|---|---|
| 0 | "apple" |
✅ |
| 1 | "banana" |
✗ |
| 2 | "cherry" |
✅ |
| 3 | "mango" |
✗ |
Result: "apple" + "cherry" = "applecherry"
Three ways to pick even indices
Since the list is always length 4, you can use direct indexing (words[0] + words[2]), slicing with step (words[::2]), or a general i % 2 == 0 check. All three give the same result here -- direct indexing is the simplest.
Solution approaches¶
Key takeaways¶
Slice with step: list[::2]
words[::2] returns every element at an even index -- words[0], words[2], words[4], … It works for any list length, not just four.
''.join(iterable) for concatenation
''.join([...]) concatenates all strings in an iterable with no separator. Cleaner than chaining + operators, especially when the number of elements can vary.
Fixed length vs. general solution
When the list is guaranteed to be length 4, words[0] + words[2] is perfectly valid and clear. For variable-length lists, prefer words[::2] or i % 2 == 0 so the logic scales.