Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Reverse a list in-place and by slicing

Return a reversed list either by creating a new reversed copy or by reversing the original list in-place.

Python practice8 minLists & TuplesBeginnerLast updated March 17, 2026

Problem statement

Write a function reverse_list(lst, in_place=False). When in_place is False (the default), return a new list that is the reversed copy of lst using slicing. When in_place is True, reverse lst in-place and return the same list object (now reversed). The function should work for lists containing any types.

Task

Implement a function that can return a reversed copy of a list or reverse the list in-place based on a flag.

Examples

Slicing reversal (default)

Input

reverse_list([1, 2, 3])

Output

[3, 2, 1]

Default behavior returns a new list reversed using slicing.

Input format

A list as the first argument and an optional boolean in_place flag.

Output format

A list that is the reversed version of the input list (either a new list or the original mutated list).

Constraints

Do not import external libraries. When in_place is True, mutate the provided list; when False, do not change the original list.

Samples

Sample 1

Input

reverse_list([1, 2, 3], True)

Output

[3, 2, 1]

The original list is reversed in-place and returned.