Problem No 11
Interleave two lists element by element
Medium≈ 15 minute session
Lesson guide
What this Python exercise practices
Interleave two lists element by element is a intermediate practice lesson that focuses on lists, iteration, filtering. It is designed to be solved in about 15 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- List values
- Basic indexing
Difficulty and time
- Level
- Intermediate
- Estimated time
- 15 minutes
Practice path
Summary
Combine two lists by alternating elements from each. If lists differ in length, append remaining elements.
Problem statement
Write a function interleave_lists(a, b) that takes two lists a and b and returns a new list formed by taking elements alternately from a and b: first element of a, first of b, second of a, second of b, and so on. If one list is longer, append the remaining tail of the longer list at the end in order. Both inputs are lists and may contain any types. Do not modify the input lists.
Task
Implement interleaving of two lists into a single list with elements taken alternately.
Examples
Even lengths
Input
a = [1, 3], b = [2, 4]
Output
[1, 2, 3, 4]
Explanation
Elements alternate: 1,2,3,4.
Input format
Two Python lists a and b.
Output format
Return a single list with elements interleaved and remaining elements appended if lengths differ.
Constraints
Do not use external libraries. Must preserve element order. Time complexity O(n) where n = len(a)+len(b).
Samples
Sample input 0
a = [1, 2, 3], b = ['x']
Sample output 0
[1, 'x', 2, 3]
Explanation 0
After taking 1 and 'x', append remaining 2 and 3.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.