In: Computer Science
Problem 2
Write a function (call it by your first name e.g. abc_trim) that takes a list as argument , modifies it by removing the first and last two elements, and returns the new modified list e.g. if we call the function as katline_trim([1,4,6,8,11, 15]), we should get [4,6,8]. (Hint: Look at how we refer to list elements by their index and also how we use a negative number as index. You can also use a loop with the len() method
NB: Include python code, please. use spider
I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments (read them for better understanding).
Images of the Code:
Note: If the below code is missing indentation please refer
code Images
Typed Code:
# Creating a function named abc_trim
# with input parameter a List
def abc_trim(List):
# removing the first element
# using index 0 points to the first element
List.pop(0)
# removing the second last element
# using negative index -2 points to the second last element
List.pop(-2)
# removing the last element
# using negative index -1 points to the last element
List.pop(-1)
# returning the trimmed List
return List
# A List of integers
List = [1,4,6,8,11, 15]
# calling function and printing the returned List
print("Trimmed List:",abc_trim(List))
#code ended here
Output:
If You Have Any Doubts. Please Ask Using Comments.
Have A Great Day!