Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Problem No 14

Map a function over a list

Medium

16 minute session

Summary

Apply a function to each element of a list and return a new list of results.

Problem statement

Given a list and a function that accepts a single argument, return a new list containing the result of applying the function to each element of the input list. Do not mutate the original list. The function may change the element type (e.g., int -> str).

Task

Implement map_list(lst, func) that returns a new list where each element is func(original_element).

Examples

Square numbers

Input

map_list([1, 2, 3], lambda x: x * x)

Output

[1, 4, 9]

Explanation

Each element is replaced by its square.

Input format

A list (lst) and a single-argument callable (func).

Output format

A list of the same length where the i-th element is func(lst[i]).

Constraints

Do not use external libraries. Maintain order. Time complexity O(n).

Samples

Sample input 0

map_list(['a','b'], lambda s: s.upper())

Sample output 0

['A', 'B']

Explanation 0

Each string is converted to uppercase.

Code editor
Loading editor…

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.

09:03 PM

Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.