First and Last Elements of List
You are given an integer N as input. Write a program to read N integers and print a list containing the first and last two inputs.
The first line of input is an integer N. The next N lines each contains an integer.
Explanation:
In the given example, we are given
6 numbers 1, 2, 3, 4, 5, 6 as input.
The list should contain first two integers 1, 2 and last two integers 5, 6 So, the output should be [1, 2, 5, 6].
Sample Input 1:
6
1
2
3
4
5
6
Sample Output 1:
[1, 2, 5, 6]
Sample Input 2:
5
1
11
13
21
19
Sample Output 2:
[1, 11, 21, 19]
n = int(input())
l = []
for i in range(n):
x = int(input())
l.append(x)
ans = [l[0], l[1], l[-2], l[-1]]
print(ans)
Input 1:
6
1
2
3
4
5
6
Output 1:
[1, 2, 5, 6]
Comments
Leave a comment