Python Python Collections Slices Slice Functions

Miha Puc
Miha Puc
1,127 Points

How is this def reverse_evens(a): e=a[::2] e.sort(reverse=True) return e

My code achives what the task requires does it not?

slices.py
def first_4(a):
    c=a[0:4]
    return c

def first_and_last_4(a):
    c=a[0:4] 
    l=c+a[-4:]
    return l


def odds(a): 
    d=a[1::2]
    return d

def reverse_evens(a):
    e=a[::2]
    e.sort(reverse=True)
    return e

1 Answer

  1. Your answer is right if all your inputs are in sorted order.
  2. the challange is not asking to sort the list. Instead it is asking to show the elements in reverse order
  3. the challenge is about slicing the list so better use slicing options to reverse [::-1] reverses the list elements
  4. Your answer will be right if you just use reverse() method on e instead of sort(reverse=true)