Answer to Question #224693 in Python for kaavya

Question #224693

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.

Input:

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]




1
Expert's answer
2021-08-11T03:20:14-0400
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]

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS