Python
Implement function swapFL() that takes a list as input and swaps the first and last ele- ments of the list. You may assume the list will be nonempty. The function should return the new list.
>>> ingredients = ['flour', 'sugar', 'butter', 'apples']
>>> swapFL(ingredients)
>>> ingredients
['apples', 'sugar', 'butter', 'flour']
def swapFL(ingredients):
#get length of list
i=len(ingredients)
#swap fiirst and last value
temp=ingredients[0]
ingredients[0]=ingredients[i-1]
ingredients[i-1]=temp
ingredients = ['flour', 'sugar', 'butter', 'apples']
print("Before swap ",ingredients)
#function call
swapFL(ingredients)
print("After swap ",ingredients)
Get Answers For Free
Most questions answered within 1 hours.