Kickstart 2020 with new opportunities! - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Searching
Sorting – sort()
Sorting – sorted()
Sorting – reverse() and reversed()
13. reverse()
Summary

Instruction

Excellent! Next, there is a function named reverse() that – as the name suggests – reverses the order of elements in a list. Take a look:

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

Note that reverse() works in place – i.e., it modifies the original variable. After this invocation, the books list will have its elements in the following order:

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

Exercise

We define a list as symmetrical if reversing the list yields the same list, e.g.:

  • Symmetrical: [1, 2, 3, 2, 1]
  • Not symmetrical: [1, 2, 3, 4]

Write a function named is_symmetrical(input_list) that returns True if input_list is symmetrical and False otherwise. Do not modify the original list.

Stuck? Here's a hint!

Because reverse() works in place, you'll need to create a copy of the list with list(input_list) and reverse the copy. Then, compare the two lists.