Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Problem No 10

Convert between lists and tuples

Easy

8 minute session

Summary

Learn to convert a list to a tuple and a tuple to a list. Useful when APIs require a specific sequence type.

Problem statement

Write a function convert_between_list_and_tuple(collection) that takes one argument which may be a list or a tuple. If the input is a list, return a tuple containing the same elements in the same order. If the input is a tuple, return a list containing the same elements in the same order. For any other input type, return None. Only a single-level conversion is required (do not deep-convert nested structures).

Task

Write a function that converts lists to tuples and tuples to lists. For other types return None.

Examples

List to tuple example

Input

[1, 2, 3]

Output

(1, 2, 3)

Explanation

A list [1,2,3] should be converted into a tuple (1,2,3).

Input format

A single Python argument which is either a list or a tuple (or other type).

Output format

Return a tuple if input was a list, a list if input was a tuple, or None for other types.

Constraints

Do not modify the input in-place. Preserve element order. Only convert the top-level collection; do not deep-convert nested elements.

Samples

Sample input 0

[1, 2]

Sample output 0

(1, 2)

Explanation 0

Converts list to tuple.

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.

10:43 PM

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