Python Python Collections Slices Slice Functions

Diego Carnavalli Bolsoni
Diego Carnavalli Bolsoni
5,437 Points

reverse even index: did it right but can't proceed. Anyone can help me with this?

when i pass a list as the exemple [1,2,3,4,5] i got the right answer as [5,3,1] but when i click on CheckWork, i can't proceed. Why?

this is the question stem: Make a function named reverse_evens that accepts a single iterable as an argument. Return every item in the iterable with an even index...in reverse.

For example, with [1, 2, 3, 4, 5] as the input, the function would return [5, 3, 1].

You can do it!

slices.py
def first_4(lista):
    return (lista[:4])

def first_and_last_4(lista):
    last_four = len(lista) - 4
    return (lista[:4] + lista[last_four:])

def odds(lista):
    return(lista[1::2])

def reverse_evens(lista):
    return(lista[-1::-2])

2 Answers

Diego Carnavalli Bolsoni
Diego Carnavalli Bolsoni
5,437 Points

I got it now, thanks Matt.

Chris Freeman suggested to use this function

def reverse_evens_2(items):
    return items[::2][::-1]

but I use this (below) because was easier for me to undertand where I did it wrong

def reverse_evens(lista):
    evens = lista[0::2]
    evens.reverse()
    return(evens)