27.2k views
1 vote
Write a function swap that swaps the first and last elements of a list argument. Sample output with input: 'all,good,things,must,end,here'

2 Answers

5 votes

Answer:

def swap (values_list):

values_list[0],values_list[-1]=values_list[-1],values_list[0]

return values_list

Step-by-step explanation:

5 votes

Answer:

def swap(lst):

lst = lst.split(",");

temp = lst[0]

lst[0] = lst[-1]

lst[-1] = temp

return lst

lst = input("Enter the list items: ")

print(swap(lst))

Step-by-step explanation:

Create a function named swap that takes one parameter, lst

Since the input is entered as commas between values, we need to split the values using split() function

Set the temp as the first item in the lst

Update the first item as the last item

Update the last item as temp (Note that temp has the initial value of the first item)

Return the list

Ask the user to enter the items

Call the swap function passing the input as parameter and print

User Evgueni
by
5.2k points