Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Searching
Sorting – sort()
Sorting – sorted()
Sorting – reverse() and reversed()
14. reversed()
Summary

Instruction

Very nice! There is also a function similar to reverse() named reversed(). The difference is that reversed() doesn't modify the original list. Here's how it's used:

books = [
  'Sound of Steel',
  'Silent Duty',
  'Marked for Crime',
  'Faceless Tribunal',
  'Enemy of Evil',
  'Shot for Gold',
  'Beyond the Storm'
]

reversed_books = []

for item in reversed(books):
  reversed_books.append(item)

As you can see, reversed() works a bit differently than sorted() – it is used to iterate over the items of a list in the reverse order when we use a for loop. After the for loop in the code above, the reversed_books variable will contain all items from books in reverse order.

Exercise

Write a function named print_squared_reversed(input_list) that accepts a list of numbers, squares each number in the list, and prints the squared numbers in reverse order.

For example, given the input [1, 2, 3, 4], print:

16
9
4
1

Stuck? Here's a hint!

Use the following for loop:

for item in reversed(input_list):