Menu Close

Interchange first and last number in list

Write a program to interchange or swap the first and last number in the list

Input:

  1. The size of the list
  2. List Elements

Output:

The new list with first and last numbers exchanged

Refer sample input and output for formatting specification.

N=int(input("Enter the size of the list: "))
l1=[]
print("Enter the elements: ")
for i in range(N):
  l1.append(int(input('')))
l1[0],l1[N-1]=l1[N-1],l1[0]
print('New list is:')
print(l1)

Input_1:
Enter the size of the list: 5
Enter the elements:
90
80
12
25
9
Output:
New list is:
[9, 80, 12, 25, 90]


Input_2:
Enter the size of the list: 4
Enter the elements:
25
23
9
20
Output:
New list is:
[20, 23, 9, 25]


Exec. on linux

Morae!Q!