Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Interleave two lists element by element

Combine two lists by alternating elements from each. If lists differ in length, append remaining elements.

Python practice15 minLists & TuplesIntermediateLast updated March 17, 2026

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]

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 1

Input

a = [1, 2, 3], b = ['x']

Output

[1, 'x', 2, 3]

After taking 1 and 'x', append remaining 2 and 3.