Hướng dẫn first and last element of list python - phần tử đầu tiên và cuối cùng của danh sách python

Đôi khi, có thể cần phải có phạm vi giữa một số nằm trong danh sách, cho các ứng dụng đó chúng tôi yêu cầu để có được yếu tố đầu tiên và cuối cùng của danh sách. Hãy để thảo luận về các cách nhất định để có được yếu tố đầu tiên và cuối cùng của danh sách. & NBSP;

Phương pháp số 1: Sử dụng chỉ mục danh sách

Sử dụng các chỉ số danh sách bên trong danh sách chính có thể thực hiện nhiệm vụ cụ thể này. Đây là phương pháp ngây thơ nhất để đạt được nhiệm vụ cụ thể này mà người ta có thể nghĩ đến. & NBSP;

Python3

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
1
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
2
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
3
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
4
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
5
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
6
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
5
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
8
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
5
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
0
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
5
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
223

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
4
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
5
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
6
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
7
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
8
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
9

Các

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
4
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
5
Input : [12, 35, 9, 56, 24]
Output : [24, 35, 9, 56, 12]

Input : [1, 2, 3]
Output : [3, 2, 1]
0
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
7
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
8
Input : [12, 35, 9, 56, 24]
Output : [24, 35, 9, 56, 12]

Input : [1, 2, 3]
Output : [3, 2, 1]
3

Output:

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]

Phương pháp số 2: Sử dụng Danh sách cắt

Người ta cũng có thể sử dụng kỹ thuật cắt danh sách để thực hiện nhiệm vụ cụ thể là có được yếu tố đầu tiên và cuối cùng. Chúng ta có thể sử dụng bước của toàn bộ danh sách để bỏ qua phần tử cuối cùng sau phần tử đầu tiên. & NBSP;

Python3

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
1
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
2
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
3
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
4
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
5
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
6
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
5
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
8
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
5
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
0
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
5
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
223

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
4
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
5
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
6
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
7
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
8
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
9

Các

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
4
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
5
Input : [12, 35, 9, 56, 24]
Output : [24, 35, 9, 56, 12]

Input : [1, 2, 3]
Output : [3, 2, 1]
0
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
7
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
8
Input : [12, 35, 9, 56, 24]
Output : [24, 35, 9, 56, 12]

Input : [1, 2, 3]
Output : [3, 2, 1]
3

Output:

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]

Phương pháp số 2: Sử dụng Danh sách cắt

Người ta cũng có thể sử dụng kỹ thuật cắt danh sách để thực hiện nhiệm vụ cụ thể là có được yếu tố đầu tiên và cuối cùng. Chúng ta có thể sử dụng bước của toàn bộ danh sách để bỏ qua phần tử cuối cùng sau phần tử đầu tiên. & NBSP;

Python3

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
1
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
2
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
3
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
4
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
5
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
6
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
5
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
8
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
5
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
0
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
5
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
223

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
4
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
5
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
6
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
7
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
8
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
9

Các

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
4
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
5
Input : [12, 35, 9, 56, 24]
Output : [24, 35, 9, 56, 12]

Input : [1, 2, 3]
Output : [3, 2, 1]
0
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
7
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
8
Input : [12, 35, 9, 56, 24]
Output : [24, 35, 9, 56, 12]

Input : [1, 2, 3]
Output : [3, 2, 1]
3

Output:

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]

Phương pháp số 2: Sử dụng Danh sách cắtmethod 

Người ta cũng có thể sử dụng kỹ thuật cắt danh sách để thực hiện nhiệm vụ cụ thể là có được yếu tố đầu tiên và cuối cùng. Chúng ta có thể sử dụng bước của toàn bộ danh sách để bỏ qua phần tử cuối cùng sau phần tử đầu tiên. & NBSP;

Các

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
14

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
15

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
16

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
17

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
18

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
19

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
20

Phương pháp số 3: Sử dụng danh sách hiểu

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
24

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
15

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
26

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
27

Danh sách hiểu có thể được sử dụng để cung cấp tốc ký cho kỹ thuật vòng lặp để tìm các yếu tố đầu tiên và cuối cùng của danh sách. Phương pháp tìm kiếm ngây thơ được chuyển đổi thành một dòng bằng phương pháp này. & NBSP;

Output:

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are :  1 4

Nhận phần tử đầu tiên của ArrayList khi sử dụng phương thức GET (INDEX) bằng cách truyền chỉ mục = 0. Nhận phần tử cuối cùng của ArrayList khi sử dụng phương thức GET (INDEX) bằng cách truyền chỉ số = kích thước - 1.

Examples:  

Input : [12, 35, 9, 56, 24]
Output : [24, 35, 9, 56, 12]

Input : [1, 2, 3]
Output : [3, 2, 1]

Làm thế nào để tôi có được phần cuối cùng của một danh sách trong Python? Find the length of the list and simply swap the first element with (n-1)th element.

Python3

pop () để có được phần tử cuối cùng của danh sách bằng danh sách. pop (), danh sách. Phương thức pop () được sử dụng để truy cập phần tử cuối cùng của danh sách.

Đưa ra một danh sách, hãy viết một chương trình Python để trao đổi yếu tố đầu tiên và cuối cùng của danh sách.

Cách tiếp cận số 1: Tìm độ dài của danh sách và chỉ cần trao đổi phần tử đầu tiên với phần tử (n-1).

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
31
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
32

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
33
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
34
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
2
[24, 35, 9, 56, 12]
6
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
37

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
33
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
39
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
2
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
41
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are :  1 4
3
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
3

Is

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
4
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
77

Output:

[24, 35, 9, 56, 12]

Cách tiếp cận #5: Trao đổi các yếu tố đầu tiên và cuối cùng là sử dụng danh sách chức năng sẵn có.pop (). Pop phần tử đầu tiên và lưu trữ nó trong một biến. Tương tự, bật phần tử cuối cùng và lưu trữ nó trong một biến khác. Bây giờ chèn hai phần tử popped ở vị trí ban đầu của nhau. & NBSP; The last element of the list can be referred as list[-1]. Therefore, we can simply swap list[0] with list[-1].

The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]33The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 419The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]2 The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]14The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 4222The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 43The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 424

pop () để có được phần tử cuối cùng của danh sách bằng danh sách. pop (), danh sách. Phương thức pop () được sử dụng để truy cập phần tử cuối cùng của danh sách.

Đưa ra một danh sách, hãy viết một chương trình Python để trao đổi yếu tố đầu tiên và cuối cùng của danh sách.

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
33
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
39
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
2
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
41
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are :  1 4
3
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
3

Is

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
4
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
77

Output:

[24, 35, 9, 56, 12]

Cách tiếp cận #5: Trao đổi các yếu tố đầu tiên và cuối cùng là sử dụng danh sách chức năng sẵn có.pop (). Pop phần tử đầu tiên và lưu trữ nó trong một biến. Tương tự, bật phần tử cuối cùng và lưu trữ nó trong một biến khác. Bây giờ chèn hai phần tử popped ở vị trí ban đầu của nhau. & NBSP; Swap the first and last element is using tuple variable. Store the first and last element as a pair in a tuple variable, say get, and unpack those elements with first and last element in that list. Now, the First and last values in that list are swapped. 

Python3

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
31
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
13
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
14
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
15

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
33
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
85
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
70
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
87
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
2
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
14

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
33
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
14
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
2
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
93
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
70
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
95

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
33
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
61
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
14

Is

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
4
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
77

Output:

[24, 35, 9, 56, 12]

Cách tiếp cận số 4: Sử dụng * Toán tử. & NBSP; Toán tử này đề xuất một thay đổi đối với cú pháp giải nén có thể giải quyết được, cho phép chỉ định một tên bắt giữ của tất cả các tên gọi sẽ được gán một danh sách tất cả các mục không được gán cho một tên thông thường. & NBSP; Using * operand. 
This operand proposes a change to iterable unpacking syntax, allowing to specify a “catch-all” name which will be assigned a list of all items not assigned to a “regular” name. 

Python3

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
14
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
2
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
3
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
4
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
5____263
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
5
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
65
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
5
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
223

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
69
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
70
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
71
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
2
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
14

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
4
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
75

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
4
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
77

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
4
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
79

Output:  

1
[2, 3]
4

Bây giờ, hãy để Lừa xem việc thực hiện phương pháp trên: & nbsp;

Python3

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
31
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
13
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
14
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
15

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
33
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
85
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
70
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
87
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
2
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
14

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
33
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
14
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
2
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
93
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
70
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
95

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
33
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
61
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
14

Is

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
4
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
77

Output:

[24, 35, 9, 56, 12]

Cách tiếp cận #5: Trao đổi các yếu tố đầu tiên và cuối cùng là sử dụng danh sách chức năng sẵn có.pop (). Pop phần tử đầu tiên và lưu trữ nó trong một biến. Tương tự, bật phần tử cuối cùng và lưu trữ nó trong một biến khác. Bây giờ chèn hai phần tử popped ở vị trí ban đầu của nhau. & NBSP; Swap the first and last elements is to use the inbuilt function list.pop(). Pop the first element and store it in a variable. Similarly, pop the last element and store it in another variable. Now insert the two popped element at each other’s original position. 

Python3

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
31
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
13
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
14
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
15

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
33
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
85
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
70
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
87
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
2
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
14

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
33
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
14
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
2
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
93
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
70
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
95

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
33
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
14
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are :  1 4
35
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are :  1 4
3
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are :  1 4
37

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
33
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
14
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are :  1 4
40

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
33
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
61
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
14

Is

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
4
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
77

Output:  

[24, 35, 9, 56, 12]

Làm thế nào để tôi tìm thấy yếu tố đầu tiên và cuối cùng của một danh sách trong Python?

Phần tử đầu tiên được truy cập bằng cách sử dụng giá trị trống trước khi đại tràng đầu tiên và phần tử cuối cùng được truy cập bằng cách chỉ định Len () với -1 làm đầu vào..

Yếu tố đầu tiên trong danh sách Python là gì?

Danh sách Python được chỉ số 0.Vì vậy, phần tử đầu tiên là 0, thứ hai là 1, vì vậy.Vì vậy, nếu có n phần tử trong một danh sách, phần tử cuối cùng là N-1.0-indexed. So the first element is 0, second is 1, so on. So if the there are n elements in a list, the last element is n-1.

Làm cách nào để tìm phần tử đầu tiên trong danh sách?

Nhận phần tử đầu tiên của ArrayList khi sử dụng phương thức GET (INDEX) bằng cách truyền chỉ mục = 0. Nhận phần tử cuối cùng của ArrayList với phương thức sử dụng GET (INDEX) bằng cách truyền chỉ số = kích thước - 1.get(index) method by passing index = 0. Get the last element of ArrayList with use of get(index) method by passing index = size – 1.

Làm thế nào để tôi có được phần cuối cùng của một danh sách trong Python?

pop () để có được phần tử cuối cùng của danh sách bằng danh sách.pop (), danh sách.Phương thức pop () được sử dụng để truy cập phần tử cuối cùng của danh sách. To get the last element of the list using list. pop(), the list. pop() method is used to access the last element of the list.