Lesson guide
What this Python exercise practices
Convert between lists and tuples is a beginner practice lesson that focuses on lists, iteration, filtering. It is designed to be solved in about 8 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- List values
- Basic indexing
Difficulty and time
- Level
- Beginner
- Estimated time
- 8 minutes
Practice path
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.
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.