Python Python Collections Slices Slice Functions

Aishwarya Manicka Ravichandran
Aishwarya Manicka Ravichandran
Python Web Development Techdegree Student 1,376 Points

Getting an error : Did'nt get the values right for first_and_last_4

So, this is my code : new_list = [] def first_4(given_list): new_list = given_list[0:4:1] return new_list

def first_and_last_4(given_list): new_list_1 = given_list[-4:] new_list.extend(new_list_1) return new_list

list_given = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] first_4(list_given) first_and_last_4(list_given)

This code works perfectly in workspace but yet getting an error stating that values are'nt right. I cant understand why. Thanks in advance

slices.py
new_list = []
def first_4(given_list):
    new_list = given_list[0:4:1]
    return new_list

def first_and_last_4(given_list):
    new_list_1 = given_list[-4:]
    new_list.extend(new_list_1)
    return new_list

list_given = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
first_4(list_given)
first_and_last_4(list_given)

1 Answer

Your "first_and_last_4" function is only returning the last 4. Try calling "first_4" and adding the 2 lists together.

def first_and_last_4(given_list):
    return first_4(given_list) + given_list[-4:]