Hướng dẫn can a list append a list python? - danh sách có thể nối thêm danh sách python không?

Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về phương thức Danh sách Python () với sự trợ giúp của các ví dụ.

Phương thức

list.append(item)
2 thêm một mục vào cuối danh sách.

Thí dụ

currencies = ['Dollar', 'Euro', 'Pound']

# append 'Yen' to the list currencies.append('Yen')

print(currencies)

# Output: ['Dollar', 'Euro', 'Pound', 'Yen']


Cú pháp của danh sách phụ lục ()

Cú pháp của phương thức

list.append(item)
2 là:

list.append(item)

tham số nối ()

Phương thức lấy một đối số duy nhất

  • Mục - một mục (số, chuỗi, danh sách, v.v.) sẽ được thêm vào cuối danh sách

Trả về giá trị từ append ()

Phương thức không trả về bất kỳ giá trị nào (trả về

list.append(item)
4).


Ví dụ 1: Thêm phần tử vào danh sách

# animals list
animals = ['cat', 'dog', 'rabbit']

# Add 'guinea pig' to the list animals.append('guinea pig')

print('Updated animals list: ', animals)

Đầu ra

Updated animals list:  ['cat', 'dog', 'rabbit', 'guinea pig']

Ví dụ 2: Thêm danh sách vào danh sách

# animals list
animals = ['cat', 'dog', 'rabbit']

# list of wild animals
wild_animals = ['tiger', 'fox']

# appending wild_animals list to animals animals.append(wild_animals)

print('Updated animals list: ', animals)

Đầu ra

Updated animals list:  ['cat', 'dog', 'rabbit', ['tiger', 'fox']]

Ví dụ 2: Thêm danh sách vào danh sách

Trong chương trình, một mục duy nhất (danh sách Wild_animals) được thêm vào danh sách động vật. If you need to add items of a list (rather than the list itself) to another list, use the extend() method.

Trong bài viết này, chúng tôi sẽ đề cập đến danh sách Python và mở rộng danh sách Python và sẽ cố gắng hiểu sự khác biệt giữa các phương thức danh sách Python, nối lại và mở rộng.difference between Python’s list methods append and extend.

Phụ lục trong Python là gì?Append in Python?

Chức năng Python sườn nối () chèn một phần tử vào một danh sách hiện có. Phần tử sẽ được thêm vào cuối danh sách cũ thay vì được đưa trở lại danh sách mới. Thêm đối số của nó như một yếu tố duy nhất vào cuối danh sách. Độ dài của danh sách tăng thêm một. & NBSP;

& nbsp; cú pháp của append () trong python

# Adds an object (a number, a string or a 
# another list) at the end of my_list
my_list.append(object)

Ví dụ 1:

Python3

list.append(item)
5
list.append(item)
6
list.append(item)
7
list.append(item)
8
list.append(item)
9
# animals list
animals = ['cat', 'dog', 'rabbit']

# Add 'guinea pig' to the list animals.append('guinea pig')

print('Updated animals list: ', animals)
0
# animals list
animals = ['cat', 'dog', 'rabbit']

# Add 'guinea pig' to the list animals.append('guinea pig')

print('Updated animals list: ', animals)
1

# animals list
animals = ['cat', 'dog', 'rabbit']

# Add 'guinea pig' to the list animals.append('guinea pig')

print('Updated animals list: ', animals)
2
list.append(item)
8
# animals list
animals = ['cat', 'dog', 'rabbit']

# Add 'guinea pig' to the list animals.append('guinea pig')

print('Updated animals list: ', animals)
4

# animals list
animals = ['cat', 'dog', 'rabbit']

# Add 'guinea pig' to the list animals.append('guinea pig')

print('Updated animals list: ', animals)
5
# animals list
animals = ['cat', 'dog', 'rabbit']

# Add 'guinea pig' to the list animals.append('guinea pig')

print('Updated animals list: ', animals)
6

Output:

['geeks', 'for', 'geeks']

Lưu ý: Danh sách là một đối tượng. Nếu bạn nối một danh sách khác vào danh sách, danh sách tham số sẽ là một đối tượng duy nhất ở cuối danh sách. & NBSP; A list is an object. If you append another list onto a list, the parameter list will be a single object at the end of the list. 

Ví dụ 2:

Python3

list.append(item)
5
list.append(item)
6
list.append(item)
7
list.append(item)
8
list.append(item)
9
# animals list
animals = ['cat', 'dog', 'rabbit']

# Add 'guinea pig' to the list animals.append('guinea pig')

print('Updated animals list: ', animals)
0
list.append(item)
9
list.append(item)
8
# animals list
animals = ['cat', 'dog', 'rabbit']

# Add 'guinea pig' to the list animals.append('guinea pig')

print('Updated animals list: ', animals)
1

Updated animals list:  ['cat', 'dog', 'rabbit', 'guinea pig']
6
list.append(item)
6
list.append(item)
7
Updated animals list:  ['cat', 'dog', 'rabbit', 'guinea pig']
9
list.append(item)
9
# animals list
animals = ['cat', 'dog', 'rabbit']

# list of wild animals
wild_animals = ['tiger', 'fox']

# appending wild_animals list to animals animals.append(wild_animals)

print('Updated animals list: ', animals)
1
list.append(item)
9
# animals list
animals = ['cat', 'dog', 'rabbit']

# list of wild animals
wild_animals = ['tiger', 'fox']

# appending wild_animals list to animals animals.append(wild_animals)

print('Updated animals list: ', animals)
3
list.append(item)
9
# animals list
animals = ['cat', 'dog', 'rabbit']

# list of wild animals
wild_animals = ['tiger', 'fox']

# appending wild_animals list to animals animals.append(wild_animals)

print('Updated animals list: ', animals)
5
# animals list
animals = ['cat', 'dog', 'rabbit']

# Add 'guinea pig' to the list animals.append('guinea pig')

print('Updated animals list: ', animals)
1

# animals list
animals = ['cat', 'dog', 'rabbit']

# list of wild animals
wild_animals = ['tiger', 'fox']

# appending wild_animals list to animals animals.append(wild_animals)

print('Updated animals list: ', animals)
7

# animals list
animals = ['cat', 'dog', 'rabbit']

# Add 'guinea pig' to the list animals.append('guinea pig')

print('Updated animals list: ', animals)
5
# animals list
animals = ['cat', 'dog', 'rabbit']

# Add 'guinea pig' to the list animals.append('guinea pig')

print('Updated animals list: ', animals)
6

Output:

['geeks', 'for', 'geeks', [6, 0, 4, 1]]

Lưu ý: Danh sách là một đối tượng. Nếu bạn nối một danh sách khác vào danh sách, danh sách tham số sẽ là một đối tượng duy nhất ở cuối danh sách. & NBSP;extend() in Python? 

Ví dụ 2:

list.append(item)5list.append(item)6 list.append(item)7list.append(item)8list.append(item)9# animals list animals = ['cat', 'dog', 'rabbit'] # Add 'guinea pig' to the list animals.append('guinea pig') print('Updated animals list: ', animals)0list.append(item)9list.append(item)8# animals list animals = ['cat', 'dog', 'rabbit'] # Add 'guinea pig' to the list animals.append('guinea pig') print('Updated animals list: ', animals)1

# Each element of an iterable gets appended 
# to my_list
my_list.extend(iterable) 

Ví dụ 1:

Python3

list.append(item)
5
list.append(item)
6
list.append(item)
7
list.append(item)
8
list.append(item)
9
# animals list
animals = ['cat', 'dog', 'rabbit']

# Add 'guinea pig' to the list animals.append('guinea pig')

print('Updated animals list: ', animals)
0
# animals list
animals = ['cat', 'dog', 'rabbit']

# Add 'guinea pig' to the list animals.append('guinea pig')

print('Updated animals list: ', animals)
1

Updated animals list:  ['cat', 'dog', 'rabbit', 'guinea pig']
6
list.append(item)
6
list.append(item)
7
Updated animals list:  ['cat', 'dog', 'rabbit', 'guinea pig']
9
list.append(item)
9
# animals list
animals = ['cat', 'dog', 'rabbit']

# list of wild animals
wild_animals = ['tiger', 'fox']

# appending wild_animals list to animals animals.append(wild_animals)

print('Updated animals list: ', animals)
1
list.append(item)
9
# animals list
animals = ['cat', 'dog', 'rabbit']

# list of wild animals
wild_animals = ['tiger', 'fox']

# appending wild_animals list to animals animals.append(wild_animals)

print('Updated animals list: ', animals)
3
list.append(item)
9
# animals list
animals = ['cat', 'dog', 'rabbit']

# list of wild animals
wild_animals = ['tiger', 'fox']

# appending wild_animals list to animals animals.append(wild_animals)

print('Updated animals list: ', animals)
5
# animals list
animals = ['cat', 'dog', 'rabbit']

# Add 'guinea pig' to the list animals.append('guinea pig')

print('Updated animals list: ', animals)
1

# Adds an object (a number, a string or a 
# another list) at the end of my_list
my_list.append(object)
8

# animals list
animals = ['cat', 'dog', 'rabbit']

# Add 'guinea pig' to the list animals.append('guinea pig')

print('Updated animals list: ', animals)
5
# animals list
animals = ['cat', 'dog', 'rabbit']

# Add 'guinea pig' to the list animals.append('guinea pig')

print('Updated animals list: ', animals)
6

Output:

list.append(item)
0

Lưu ý: Danh sách là một đối tượng. Nếu bạn nối một danh sách khác vào danh sách, danh sách tham số sẽ là một đối tượng duy nhất ở cuối danh sách. & NBSP; A string is iterable, so if you extend a list with a string, you’ll append each character as you iterate over the string. 

Ví dụ 2:

Python3

list.append(item)
5
list.append(item)
6
list.append(item)
7
list.append(item)
8
list.append(item)
9
# animals list
animals = ['cat', 'dog', 'rabbit']

# Add 'guinea pig' to the list animals.append('guinea pig')

print('Updated animals list: ', animals)
0
list.append(item)
9
list.append(item)
8
# animals list
animals = ['cat', 'dog', 'rabbit']

# Add 'guinea pig' to the list animals.append('guinea pig')

print('Updated animals list: ', animals)
1

['geeks', 'for', 'geeks', [6, 0, 4, 1]]
6
list.append(item)
8
# animals list
animals = ['cat', 'dog', 'rabbit']

# Add 'guinea pig' to the list animals.append('guinea pig')

print('Updated animals list: ', animals)
4

# animals list
animals = ['cat', 'dog', 'rabbit']

# Add 'guinea pig' to the list animals.append('guinea pig')

print('Updated animals list: ', animals)
5
# animals list
animals = ['cat', 'dog', 'rabbit']

# Add 'guinea pig' to the list animals.append('guinea pig')

print('Updated animals list: ', animals)
6

Output:

list.append(item)
1

Lưu ý: Danh sách là một đối tượng. Nếu bạn nối một danh sách khác vào danh sách, danh sách tham số sẽ là một đối tượng duy nhất ở cuối danh sách. & NBSP; Append has constant time complexity i.e.,O(1). Extend has a time complexity of O(k). Where k is the length of the list which need to be added.


Bạn có thể sử dụng += để nối một danh sách không?

Đối với một danh sách, += giống như phương thức mở rộng hơn là phương thức nối.Với một danh sách ở bên trái của toán tử +=, một danh sách khác là cần thiết ở bên phải của toán tử.Tất cả các mục trong danh sách bên phải của toán tử được thêm vào cuối danh sách được tham chiếu đến bên trái của toán tử.+= is more like the extend method than like the append method. With a list to the left of the += operator, another list is needed to the right of the operator. All the items in the list to the right of the operator get added to the end of the list that is referenced to the left of the operator.

Hai danh sách có thể được thêm vào trong Python không?

Có một số cách để tham gia, hoặc concatenate, hai hoặc nhiều danh sách trong Python.Một trong những cách dễ nhất là sử dụng toán tử +.One of the easiest ways are by using the + operator.

Phụ lục có tạo ra một danh sách mới không?

Phương thức append () không tạo ra một danh sách mới.Thay vào đó, danh sách ban đầu được thay đổi.append () cũng cho phép bạn thêm nội dung của một danh sách vào danh sách khác.. Instead, original list is changed. append() also lets you add the contents of one list to another list.