Returns Nth Element from the List in Reverse Order

Viewed 36

How to write a function fifth_element that takes every fifth element from the list in reverse order and returns a list of elements?

For example:

def fifth_element(some_list: list) -> list:
    ...


some_list = ['e',6,8,'A','>','^','S','$','R','C',6,'+','#',9,'/',1,'T','!','%','K',7,'-','O','*','<',2,'h',4,'g']

1 Answers
def fifth_element(my_list):
    return my_list[len(my_list)::-5]         

The Len(my_list) allows you to get the total length of the list and the splicing notation is stating that you want to go from the back of the list to the front of the list at the order of -5 each time

Related