Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Convert between lists and tuples

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

Python practice8 minLists & TuplesBeginnerLast updated March 17, 2026

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)

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 1

Input

[1, 2]

Output

(1, 2)

Converts list to tuple.