On January 21th at 14:15 UTC , progression through exercises will be unavailable for 10 minutes due to a planned maintenance break.
Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Iterating over a list
Modifying lists
7. Changing the order of list elements
Working with multiple lists


Good job! Another common task is to change the order of list elements. We'll show you the classic problem of reversing a list. Take a look:

sleep_durations = [368, 690, 426, 494, 445, 690, 423, 534, 606, 390]
swap_index = len(sleep_durations) - 1
for i in range(int(len(sleep_durations) / 2)):
  temp_duration = sleep_durations[i]
  sleep_durations[i] = sleep_durations[swap_index]
  sleep_durations[swap_index] = temp_duration
  swap_index -= 1

In each iteration, we want to perform a swap of two elements: the first element with the last, the second element with the next-to-last, etc. To make this happen, we need to find the indexes of both elements for the swap operation. One of the indexes is provided in the i variable; we also need another index, so we introduce the swap_index variable and decrease its value by 1 after each swap operation. The image below shows the consecutive loop iterations:



Let's do some shuffling! Change the order of elements in the monthly_spendings list by swapping pairs of neighboring elements. In other words, if the monthly_spendings list was:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

the expected result would be:

[2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11]

At the end of your code, print the modified mothly_spendings list (type print(monthly_spendings)).

Stuck? Here's a hint!

You can create a for loop with a temporary i variable and iterate over all the indexes. If an index is even (i.e., i%2 == 0), swap the positions of list[i] and list[i+1] using a temporary variable.