We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Reverse List

Assignment

Some of our players would like to view their inventory items listed in reverse.

Using a loop, let's write a function that takes a list as an input and returns a new list where all the items are in reverse order. We'll leave the original list unchanged.

For example:

[1, 2, 3] -> [3, 2, 1]
['a', 'b', 'c', 'd'] -> ['d', 'c', 'b', 'a']

Tips

The Python range function is very useful when working with lists. It lets you choose where to start and stop, and how to step through a list.

  • Remember, function arguments are separated by commas inside the parentheses. For range the function signature is range(start, stop, step). Don't forget you can use negative values for these arguments!

  • It's common to nest built-ins: you can pass len(...) directly to range(...). For example, range(len(items)) iterates over indices 0 through len(items) - 1.

  • Where should you start your loop from? Could len() be useful for giving range a starting index?

  • Where should you end your loop?

  • What should the step be? In other words, how should you increment i in each iteration of the loop?