Knowee
Questions
Features
Study Tools

Write a Python program to interchange the first and last element of the ListConstraints:Input Format:Line 1: <Read a List>Output Format:<List>Example:Input:[5,10,15,20,25,30,35,40]Output:[40, 10, 15, 20, 25, 30, 35, 5]Explanation:NAPublic Test Cases:# INPUT EXPECTED OUTPUT1 [5,10,15,20,25,30,35,40][40, 10, 15, 20, 25, 30, 35, 5]Run CodeSubmit Code12345l1=eval(input())l1=[int(x) for x in l1]if l1: l1[0],l1[-1]=l1[-1],l1[0]print(l1)OutputCustom Input

Question

Write a Python program to interchange the first and last element of the ListConstraints:Input Format:Line 1: <Read a List>Output Format:<List>Example:Input:[5,10,15,20,25,30,35,40]Output:[40, 10, 15, 20, 25, 30, 35, 5]Explanation:NAPublic Test Cases:# INPUT EXPECTED OUTPUT1 [5,10,15,20,25,30,35,40][40, 10, 15, 20, 25, 30, 35, 5]Run CodeSubmit Code12345l1=eval(input())l1=[int(x) for x in l1]if l1: l1[0],l1[-1]=l1[-1],l1[0]print(l1)OutputCustom Input

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Python program that interchanges the first and last element of a list:

# Read a list from user input
list1 = eval(input())

# Check if the list is not empty
if list1:
    # Swap the first and last elements of the list
    list1[0], list1[-1] = list1[-1], list1[0]

# Print the modified list
print(list1)

You can run this program and input a list like [5,10,15,20,25,30,35,40]. The output will be [40, 10, 15, 20, 25, 30, 35, 5], which is the original list with the first and last elements swapped.

This problem has been solved

Similar Questions

Alternate Elements in Linked ListYou are now well versed with the concepts of Linked Lists. So, now you have been given an advance coding challenge, i.e., you have to create the code on the topic linked lists such that it will give the output of only elements in alternate position from last to first and then first to last. Constraints:Input Format :First line of input contains the number of integers.Next lines are followed by the integer values.Output Format :Corresponding output.Example:Input 1:51  2  3  4  5Output 1:1  3  55  3  1 Input 2:411  12  14  13Output 2:12   1313  12  Explanation:Alternate elements from first element are 1 3 5Alternate elements from last to first are 5 3 1Public Test Cases:# INPUT EXPECTED OUTPUT1 51 2 3 4 51 3 5 5 3 1 2 4 11 12 14 1311 14 13 12

Given a list, write a Python program to swap first and last element of the list.

Test Case 1:Expected Output:list1:·10,20,30list2:·4,5,8[6,·15,·22]Test Case 2:Expected Output:list1:·15,45,65,85,96list2:·20,25,32,65,45,68,69,45Unequal·lengths

The only line contains space-separated integers, and .ConstraintsSample Input 05 4Sample Output 0233Explanation 0All possible values of and are:The maximum possible value of that is also is , so we print on first line.The maximum possible value of that is also is , so we print on second line.The maximum possible value of that is also is , so we print on third line.

Write a Python program to read two integer lists. If the lengths of the two lists are equal, create a new list that contains the absolute differences between corresponding elements of the given lists, if not, then print "Unequal lengths".Note: Please map your input and output layout according to the visible sample test cases.Sample Test CasesTest Case 1:Expected Output:list1:·10,20,30list2:·4,5,8[6,·15,·22]Test Case 2:Expected Output:list1:·15,45,65,85,96list2:·20,25,32,65,45,68,69,45Unequal·lengths

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.