Làm cách nào để lấy n trong danh sách trong python?

Bạn có thể sử dụng phép cắt để cắt danh sách đã cho và nhận danh sách mới với N phần tử đầu tiên của danh sách gốc hoặc danh sách nguồn

Hoặc, bạn có thể truy cập danh sách bằng chỉ mục và sử dụng câu lệnh lặp như vòng lặp for để lấy N phần tử đầu tiên

Một cách khác là sử dụng hiểu danh sách. Đây là một việc quá mức cần thiết để có được N phần tử đầu tiên, nhưng sẽ rất hữu ích khi học lập trình Python

Vì vậy, tùy theo yêu cầu, bạn có thể chọn một trong các cách trên để lấy hoặc tạo danh sách mới với N phần tử đầu tiên từ danh sách nguồn

Liệt kê với N phần tử đầu tiên bằng cách sử dụng Slicing

Cú pháp để lấy danh sách có N phần tử đầu tiên bằng cách cắt là

new_list = source_list[:N]

Danh sách nguồn không được sửa đổi. Một danh sách mới được tạo với N phần tử đầu tiên của danh sách nguồn

Sau đây là một chương trình ví dụ, trong đó chúng tôi khởi tạo một danh sách và sao chép N phần tử đầu tiên của danh sách sang một danh sách mới bằng cách cắt

Chương trình Python

source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = source_list[:N]
print[source_list]
print[new_list]
Chạy

đầu ra

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]

Danh sách mới chứa 4 phần tử đầu tiên của danh sách nguồn

Liệt kê với N phần tử đầu tiên bằng vòng lặp For

Để lấy N phần tử đầu tiên của danh sách, hãy sử dụng vòng lặp for với phạm vi [0, N], tạo danh sách trống mới và nối các phần tử của danh sách nguồn vào danh sách mới trong vòng lặp for

phạm vi [0, N] lặp lại từ 0 đến N-1, các bước của 1. N không được bao gồm

danh sách. append[element] nối thêm phần tử đã cho vào danh sách

Sau đây là một chương trình ví dụ, nơi chúng tôi khởi tạo một danh sách và sử dụng vòng lặp for để lấy N phần tử đầu tiên của danh sách đã cho

Chương trình Python

source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
Chạy

đầu ra

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]

Liệt kê với N phần tử đầu tiên bằng cách sử dụng tính năng hiểu danh sách

Trong chương trình sau, chúng tôi sử dụng khả năng hiểu danh sách với điều kiện if và thu thập các mục của danh sách nguồn có chỉ mục nhỏ hơn N

Chương trình Python

source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = [x for index, x in enumerate[source_list] if index < N]
print[source_list]
print[new_list]
Chạy

đầu ra

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]

Tóm lược

Trong hướng dẫn về Ví dụ Python này, chúng ta đã học cách lấy danh sách mà không có phần tử cuối cùng của nó, với sự trợ giúp của phương thức cắt và pop[]

Trước khi lấy Số lượng phần tử trong Danh sách Python, chúng ta phải tạo một Danh sách trống và lưu trữ một số mục vào danh sách

Có ba phương pháp để lấy số phần tử trong danh sách, i. e

  • Sử dụng hàm len[] trong Python
  • Sử dụng vòng lặp
  • Sử dụng hàm toán tử length_hint

Sử dụng hàm Len[] để lấy số phần tử

Chúng ta có thể sử dụng hàm len[] để trả về số phần tử có trong danh sách

Python3




[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
3

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
4
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
5
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
6
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
7
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
8
[1, 2, 3, 4]
No of elements in the list are: 4
0
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
8
[1, 2, 3, 4]
No of elements in the list are: 4
2
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
8
[1, 2, 3, 4]
No of elements in the list are: 4
4
[1, 2, 3, 4]
No of elements in the list are: 4
5

 

[1, 2, 3, 4]
No of elements in the list are: 4
6

[1, 2, 3, 4]
No of elements in the list are: 4
7____78

 

[1, 2, 3, 4]
No of elements in the list are: 4
9

source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = source_list[:N]
print[source_list]
print[new_list]
50

[1, 2, 3, 4]
No of elements in the list are: 4
7______152____153
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
8
[1, 2, 3, 4]
No of elements in the list are: 4
02
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = source_list[:N]
print[source_list]
print[new_list]
52
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
7
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
35
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
36
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
37
[1, 2, 3, 4]
No of elements in the list are: 4
08

Chúng ta thường gặp tình huống cần lấy số/chuỗi làm đầu vào từ người dùng. Trong bài viết này, chúng ta sẽ xem cách lấy danh sách đầu vào từ người dùng.  

Ví dụ.
 

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
2

Mã số 1. Ví dụ cơ bản 
 

Python3




source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
6

source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
7
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
5
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
9

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
0

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
1

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
2
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
5
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
4____360
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
61
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
60
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
63
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
64

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
0

source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
66

source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
67
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
68
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
69
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
70
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
60
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
72
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
73

source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
74
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
75
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
5
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
4
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
60
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
61
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
50

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
0

source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
74
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
53______254

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
55

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
56____257

Đầu ra.
 

  
Mã số 2. Có xử lý ngoại lệ 
 

Python3




[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
58

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
59
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
90

source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
74
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
92
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
5
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
9

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
55

source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
74
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
97
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
98
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
90

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
00
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
01
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
4
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
60
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
61
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
05

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
06

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
07

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
08
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
90

source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
74
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
56____412

Đầu ra.
 

  
Mã số 3. Sử dụng map[] 
 

Python3




[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
13

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
2
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
5
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
4____360
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
61
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
60
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
63
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
64

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
0

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
23

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
24
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
5
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
26
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
60
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
28
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
60
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
4
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
51
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
61
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
60
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
54
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
55

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
0

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
56______360
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
59
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
40

Đầu ra.
 

  
Mã số 4. Danh sách các danh sách dưới dạng đầu vào 
 

Python3




source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
7____25
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
43

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
2
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
5
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
4____360
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
61
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
60
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
63
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
64

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
0

source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
67
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
68
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
69
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
70
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
60
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
72
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
73

source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
74
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
75
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
5
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
613
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
61
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
615______44
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
60
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
61
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
619

source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
74
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
53

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
55

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
56____257

Đầu ra.
 

Mã số 5. Sử dụng tính năng hiểu danh sách và đánh máy 
 

Python3




source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
625

source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
626
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
5
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
628

[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
0

source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
630

source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
631
[8, 4, 7, 3, 6, 1, 9]
[8, 4, 7, 3]
5
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
650
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
67
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
641
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
69
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
61
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
60
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
645
source_list = [8, 4, 7, 3, 6, 1, 9]
N = 4
new_list = []
for index in range[0, N]:
    new_list.append[source_list[index]]
print[source_list]
print[new_list]
646

Chủ Đề