Hướng dẫn what is the difference between append and extend in python with example? - ví dụ về sự khác biệt giữa nối thêm và mở rộng trong python là gì?

Chuẩn bị: Bổ số đối tượng ở cuối.

x = [1, 2, 3]
x.append([4, 5])
print (x)

cung cấp cho bạn: & nbsp; [1, 2, 3, [4, 5]]]]


Mở rộng: Mở rộng danh sách bằng cách nối thêm các yếu tố từ ITBER.

x = [1, 2, 3]
x.extend([4, 5])
print (x)

Cung cấp cho bạn: & nbsp; [1, 2, 3, 4, 5]

Hy vọng nó giúp!!

Nếu bạn cần biết thêm về Python, nên tham gia & nbsp; Python Course & nbsp; hôm nay.

Thanks!

Hướng dẫn what is the difference between append and extend in python with example? - ví dụ về sự khác biệt giữa nối thêm và mở rộng trong python là gì?

🔹 Chào mừng

Nếu bạn muốn học cách làm việc với

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
1 và
# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
2 và hiểu sự khác biệt của chúng, thì bạn đã đến đúng nơi. Chúng là các phương pháp danh sách mạnh mẽ mà bạn chắc chắn sẽ sử dụng trong các dự án Python của mình.

Trong bài viết này, bạn sẽ học:

  • Làm thế nào và khi nào để sử dụng phương pháp
    # Define the list
    >>> nums = [1, 2, 3, 4]
    
    # Add the integer 5 to the end of the existing list
    >>> nums.append(5)
    
    # See the updated value of the list
    >>> nums
    [1, 2, 3, 4, 5]
    1.
  • Làm thế nào và khi nào để sử dụng phương pháp
    # Define the list
    >>> nums = [1, 2, 3, 4]
    
    # Add the integer 5 to the end of the existing list
    >>> nums.append(5)
    
    # See the updated value of the list
    >>> nums
    [1, 2, 3, 4, 5]
    2.
  • Sự khác biệt chính của họ.

Hãy bắt đầu nào. ✨

Hãy xem phương pháp

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
1 hoạt động như thế nào sau hậu trường.

Trường hợp sử dụng

Bạn nên sử dụng phương thức này khi bạn muốn thêm một mục duy nhất vào cuối danh sách.add a single item to the end of a list.

Mẹo: Bạn có thể thêm các mục của bất kỳ loại dữ liệu nào vì danh sách có thể có các yếu tố của các loại dữ liệu khác nhau.You can add items of any data type since lists can have elements of different data types.

Hướng dẫn what is the difference between append and extend in python with example? - ví dụ về sự khác biệt giữa nối thêm và mở rộng trong python là gì?

Cú pháp và đối số

Để gọi phương thức

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
1, bạn sẽ cần sử dụng cú pháp này:

Hướng dẫn what is the difference between append and extend in python with example? - ví dụ về sự khác biệt giữa nối thêm và mở rộng trong python là gì?

Từ trái sang phải:

  • Danh sách sẽ được sửa đổi. Đây thường là một biến tham chiếu một danh sách.
  • Một dấu chấm, theo sau là tên của phương thức
    # Define the list
    >>> nums = [1, 2, 3, 4]
    
    # Add the integer 5 to the end of the existing list
    >>> nums.append(5)
    
    # See the updated value of the list
    >>> nums
    [1, 2, 3, 4, 5]
    1.
  • Trong ngoặc đơn, mục sẽ được thêm vào cuối danh sách.

Mẹo: DOT là rất quan trọng. Điều này được gọi là "ký hiệu dấu chấm". Dấu chấm về cơ bản nói "Gọi phương thức này trong danh sách cụ thể này", vì vậy hiệu ứng của phương thức sẽ được áp dụng cho danh sách được đặt trước dấu chấm.Tips: The dot is very important. This is called "dot notation". The dot basically says "call this method on this particular list", so the effect of the method will be applied to the list that is located before the dot.

Ví dụ

Đây là một ví dụ về cách sử dụng

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
1:

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]

Mẹo: Khi bạn sử dụng

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
1, danh sách ban đầu được sửa đổi. Phương thức không tạo ra một bản sao của danh sách - nó làm đột biến danh sách ban đầu trong bộ nhớ.Tips: When you use
# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
1 the original list is modified. The method does not create a copy of the list – it mutates the original list in memory.

Hãy giả vờ rằng chúng tôi đang tiến hành một nghiên cứu và chúng tôi muốn phân tích dữ liệu được thu thập bằng Python. Chúng ta cần thêm một phép đo mới vào danh sách các giá trị hiện có.

Chúng ta làm điều đó như thế nào? Chúng tôi sử dụng phương pháp

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
1!

Bạn có thể thấy nó ngay tại đây:

# Existing list
>>> nums = [5.6, 7.44, 6.75, 4.56, 2.3]

# Add the float (decimal number) to the end of the existing list
>>> nums.append(7.34)

# See the updated value of the list
>>> nums
[5.6, 7.44, 6.75, 4.56, 2.3, 7.34]

Tương đương với...

Nếu bạn quen thuộc với chuỗi, danh sách hoặc cắt giảm, những gì

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
1 thực sự làm sau hậu trường là tương đương với:

a[len(a):] = [x]

Với ví dụ này, bạn có thể thấy rằng chúng tương đương.

Sử dụng

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
1:

>>> nums = [5.6, 7.44, 6.75, 4.56, 2.3]
>>> nums.append(4.52)
>>> nums
[5.6, 7.44, 6.75, 4.56, 2.3, 4.52]

Sử dụng Danh sách cắt lát:

>>> nums = [5.6, 7.44, 6.75, 4.56, 2.3]
>>> nums[len(nums):] = [4.52]
>>> nums
[5.6, 7.44, 6.75, 4.56, 2.3, 4.52]

Kết quả là như nhau, nhưng sử dụng # Define the list >>> nums = [1, 2, 3, 4] # Add the integer 5 to the end of the existing list >>> nums.append(5) # See the updated value of the list >>> nums [1, 2, 3, 4, 5]2 dễ đọc và nhỏ gọn hơn, phải không? Python thực sự cung cấp các công cụ tuyệt vời để cải thiện quy trình làm việc của chúng tôi.

Tóm tắt về sự khác biệt của họ

>>> nums = [5.6, 7.44, 6.75, 4.56, 2.3]
>>> nums.append([5.67, 7.67, 3.44])
>>> nums
# OUTPUT?

Bạn đã sẵn sàng chưa? Đây sẽ là đầu ra:

[5.6, 7.44, 6.75, 4.56, 2.3, [5.67, 7.67, 3.44]]

Bạn có thể hỏi, tại sao danh sách đầy đủ được thêm vào dưới dạng một mục? Đó là bởi vì phương thức

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
1 thêm toàn bộ mục vào cuối danh sách. Nếu mục này là một chuỗi như danh sách, từ điển hoặc tuple, toàn bộ chuỗi sẽ được thêm vào dưới dạng một mục duy nhất của danh sách hiện có.

Ở đây chúng tôi có một ví dụ khác (bên dưới). Trong trường hợp này, mục này là một tuple và nó được thêm vào dưới dạng một mục duy nhất của danh sách, không phải là các mục riêng lẻ:

>>> names = ["Lulu", "Nora", "Gino", "Bryan"]
>>> names.append(("Emily", "John"))
>>> names
['Lulu', 'Nora', 'Gino', 'Bryan', ('Emily', 'John')]

🔹 mở rộng

Bây giờ chúng ta hãy đi sâu vào chức năng của phương pháp

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
2.

Trường hợp sử dụng

Bạn nên sử dụng phương pháp này nếu bạn cần nối một số mục vào danh sách dưới dạng các mục riêng lẻ.append several items to a list as individual items.

Hãy để tôi minh họa tầm quan trọng của phương pháp này với một người bạn quen thuộc mà bạn vừa học: Phương pháp

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
1. Dựa trên những gì bạn đã học cho đến nay, nếu chúng tôi muốn thêm một số mục riêng lẻ vào danh sách bằng cách sử dụng
# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
1, chúng tôi sẽ cần sử dụng
# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
1 nhiều lần, như thế này:individual items to a list using
# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
1, we would need to use
# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
1 several times, like this:

x = [1, 2, 3]
x.extend([4, 5])
print (x)
0

Tôi chắc chắn rằng bạn có thể nghĩ rằng điều này sẽ không hiệu quả lắm, phải không? Điều gì sẽ xảy ra nếu tôi cần thêm hàng ngàn hoặc hàng triệu giá trị? Tôi không thể viết hàng ngàn hoặc hàng triệu dòng cho nhiệm vụ đơn giản này. Điều đó sẽ mất mãi mãi!

Vì vậy, chúng ta hãy xem một giải pháp thay thế. Chúng tôi có thể lưu trữ các giá trị mà chúng tôi muốn thêm vào một danh sách riêng và sau đó sử dụng một vòng lặp để gọi

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
1 nhiều lần khi cần thiết:

x = [1, 2, 3]
x.extend([4, 5])
print (x)
1

Điều này hiệu quả hơn, phải không? Chúng tôi chỉ viết một vài dòng. Nhưng có một cách thậm chí còn hiệu quả hơn, dễ đọc hơn và nhỏ gọn để đạt được cùng một mục đích:

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
2!

x = [1, 2, 3]
x.extend([4, 5])
print (x)
2

Hãy xem phương pháp này hoạt động như thế nào đằng sau hậu trường.

Cú pháp và đối số

Để gọi phương thức

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
2, bạn sẽ cần sử dụng cú pháp này:

Hướng dẫn what is the difference between append and extend in python with example? - ví dụ về sự khác biệt giữa nối thêm và mở rộng trong python là gì?

Từ trái sang phải:

  • Danh sách sẽ được sửa đổi. Đây thường là một biến đề cập đến danh sách.
  • Một dấu chấm
    a[len(a):] = [x]
    1 (cho đến nay, mọi thứ đều giống như trước đây).
  • Tên của phương pháp
    a[len(a):] = [x]
    2. (Bây giờ mọi thứ bắt đầu thay đổi ...).
  • Trong ngoặc đơn, một số khác (danh sách, tuple, từ điển, bộ hoặc chuỗi) chứa các mục sẽ được thêm vào dưới dạng các yếu tố riêng lẻ của danh sách.iterable (list, tuple, dictionary, set, or string) that contains the items that will be added as individual elements of the list.

Mẹo: Theo tài liệu của Python, một điều có thể được định nghĩa là "một đối tượng có khả năng trả lại một thành viên của mình một lần". Tererables có thể được sử dụng trong một vòng lặp và vì chúng trả lại các yếu tố của chúng mỗi lần, chúng ta có thể "làm một cái gì đó" với mỗi người trong số chúng, một lần lặp lại. According to the Python documentation, an iterable is defined as "an object capable of returning its members one at a time". Iterables can be used in a for loop and because they return their elements one at a time, we can "do something" with each one of them, one per iteration.

Đằng sau hậu trường

Hãy xem cách

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
2 hoạt động đằng sau hậu trường. Ở đây chúng tôi có một ví dụ:

x = [1, 2, 3]
x.extend([4, 5])
print (x)
3

Bạn có thể nghĩ về

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
2 như một phương pháp nối các yếu tố riêng lẻ của điều đáng tin theo cùng thứ tự như chúng xuất hiện.

Trong trường hợp này, chúng tôi có một danh sách

a[len(a):] = [x]
5 như được minh họa trong sơ đồ dưới đây. Chúng tôi cũng có một danh sách
a[len(a):] = [x]
6 chứa chuỗi các giá trị mà chúng tôi muốn thêm. Phương thức lấy từng yếu tố của
a[len(a):] = [x]
7 và nối nó vào liệt kê
a[len(a):] = [x]
8 theo cùng một thứ tự.

Hướng dẫn what is the difference between append and extend in python with example? - ví dụ về sự khác biệt giữa nối thêm và mở rộng trong python là gì?
Bước 1. Phần tử thứ nhất được nối thêm. & NBSP; Bước 2. Phần tử thứ hai được nối thêm.
Hướng dẫn what is the difference between append and extend in python with example? - ví dụ về sự khác biệt giữa nối thêm và mở rộng trong python là gì?
Step 2. Second element appended. 
Hướng dẫn what is the difference between append and extend in python with example? - ví dụ về sự khác biệt giữa nối thêm và mở rộng trong python là gì?
Step 3. Third element appended

Sau khi quá trình này được hoàn thành, chúng tôi có danh sách cập nhật

a[len(a):] = [x]
8 và chúng tôi có thể làm việc với các giá trị dưới dạng các yếu tố riêng lẻ của
a[len(a):] = [x]
8.

Hướng dẫn what is the difference between append and extend in python with example? - ví dụ về sự khác biệt giữa nối thêm và mở rộng trong python là gì?

Mẹo: Danh sách

a[len(a):] = [x]
7 được sử dụng để mở rộng danh sách
a[len(a):] = [x]
8 vẫn còn nguyên sau quá trình này. Bạn có thể làm việc với nó sau cuộc gọi đến
# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
2. Đây là bằng chứng:Tips: The list
a[len(a):] = [x]
7 used to extend list
a[len(a):] = [x]
8 remains intact after this process. You can work with it after the call to
# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
2. Here is the proof:

x = [1, 2, 3]
x.extend([4, 5])
print (x)
4

Ví dụ

Bạn có thể tò mò muốn biết phương pháp

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
2 hoạt động như thế nào khi bạn vượt qua các loại lặp khác nhau. Hãy xem làm thế nào trong các ví dụ sau:

Đối với Tuples: Quá trình hoạt động chính xác giống nhau nếu bạn vượt qua một tuple. Các yếu tố riêng lẻ của tuple được gắn từng cái một theo thứ tự chúng xuất hiện.
The process works exactly the same if you pass a tuple. The individual elements of the tuple are appended one by one in the order that they appear.

x = [1, 2, 3]
x.extend([4, 5])
print (x)
5

Đối với các bộ: Điều tương tự xảy ra nếu bạn vượt qua một bộ. Các yếu tố của tập hợp được thêm một.
The same occurs if you pass a set. The elements of the set are appended one by one.

x = [1, 2, 3]
x.extend([4, 5])
print (x)
6

Đối với chuỗi: Chuỗi hoạt động khác một chút với phương pháp

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
2. Mỗi ký tự của chuỗi được coi là một "mục", vì vậy các ký tự được thêm một thứ theo thứ tự chúng xuất hiện trong chuỗi.
Strings work a little bit different with the
# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
2 method. Each character of the string is considered an "item", so the characters are appended one by one in the order that they appear in the string.

x = [1, 2, 3]
x.extend([4, 5])
print (x)
7

Đối với từ điển: Từ điển có một hành vi cụ thể khi bạn chuyển chúng dưới dạng đối số cho

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
2. Trong trường hợp này, các khóa của từ điển được thêm từng cái một. Các giá trị của các cặp giá trị khóa tương ứng không được thêm vào.
Dictionaries have a particular behavior when you pass them as arguments to
# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
2. In this case, the keys of the dictionary are appended one by one. The values of the corresponding key-value pairs are not appended.

Trong ví dụ này (bên dưới), các khóa là "D", "E" và "F". Các giá trị này được nối vào danh sách

a[len(a):] = [x]
8.

x = [1, 2, 3]
x.extend([4, 5])
print (x)
8

Tương đương với...

Những gì

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
2 làm tương đương với
>>> nums = [5.6, 7.44, 6.75, 4.56, 2.3]
>>> nums.append(4.52)
>>> nums
[5.6, 7.44, 6.75, 4.56, 2.3, 4.52]
9. Ở đây chúng tôi có một ví dụ để minh họa rằng chúng tương đương:

Sử dụng

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
2:

x = [1, 2, 3]
x.extend([4, 5])
print (x)
9

Sử dụng Danh sách cắt lát:

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
0

Kết quả là như nhau, nhưng sử dụng

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
2 dễ đọc và nhỏ gọn hơn, phải không? Python thực sự cung cấp các công cụ tuyệt vời để cải thiện quy trình làm việc của chúng tôi.

Tóm tắt về sự khác biệt của họ

Bây giờ bạn đã biết cách làm việc với

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
1 và
# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
2, hãy xem một bản tóm tắt về sự khác biệt chính của họ:

  • Hiệu ứng:
    # Define the list
    >>> nums = [1, 2, 3, 4]
    
    # Add the integer 5 to the end of the existing list
    >>> nums.append(5)
    
    # See the updated value of the list
    >>> nums
    [1, 2, 3, 4, 5]
    1 thêm một phần tử vào cuối danh sách trong khi
    # Define the list
    >>> nums = [1, 2, 3, 4]
    
    # Add the integer 5 to the end of the existing list
    >>> nums.append(5)
    
    # See the updated value of the list
    >>> nums
    [1, 2, 3, 4, 5]
    2 có thể thêm nhiều phần tử riêng lẻ vào cuối danh sách.
    :
    # Define the list
    >>> nums = [1, 2, 3, 4]
    
    # Add the integer 5 to the end of the existing list
    >>> nums.append(5)
    
    # See the updated value of the list
    >>> nums
    [1, 2, 3, 4, 5]
    1 adds a single element to the end of the list while
    # Define the list
    >>> nums = [1, 2, 3, 4]
    
    # Add the integer 5 to the end of the existing list
    >>> nums.append(5)
    
    # See the updated value of the list
    >>> nums
    [1, 2, 3, 4, 5]
    2 can add multiple individual elements to the end of the list.
  • Đối số:
    # Define the list
    >>> nums = [1, 2, 3, 4]
    
    # Add the integer 5 to the end of the existing list
    >>> nums.append(5)
    
    # See the updated value of the list
    >>> nums
    [1, 2, 3, 4, 5]
    1 lấy một yếu tố duy nhất làm đối số trong khi
    # Define the list
    >>> nums = [1, 2, 3, 4]
    
    # Add the integer 5 to the end of the existing list
    >>> nums.append(5)
    
    # See the updated value of the list
    >>> nums
    [1, 2, 3, 4, 5]
    2 lấy một đối số có thể lặp lại (danh sách, tuple, từ điển, bộ, chuỗi).
    :
    # Define the list
    >>> nums = [1, 2, 3, 4]
    
    # Add the integer 5 to the end of the existing list
    >>> nums.append(5)
    
    # See the updated value of the list
    >>> nums
    [1, 2, 3, 4, 5]
    1 takes a single element as argument while
    # Define the list
    >>> nums = [1, 2, 3, 4]
    
    # Add the integer 5 to the end of the existing list
    >>> nums.append(5)
    
    # See the updated value of the list
    >>> nums
    [1, 2, 3, 4, 5]
    2 takes an iterable as argument (list, tuple, dictionaries, sets, strings).

Tôi thực sự hy vọng rằng bạn thích bài viết của tôi và thấy nó hữu ích. Bây giờ bạn có thể làm việc với

# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
1 và
# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
2 trong các dự án Python của bạn. Kiểm tra các khóa học trực tuyến của tôi. Theo dõi tôi trên Twitter. ⭐
Now you can work with
# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
1 and
# Define the list
>>> nums = [1, 2, 3, 4]

# Add the integer 5 to the end of the existing list
>>> nums.append(5)

# See the updated value of the list
>>> nums
[1, 2, 3, 4, 5]
2 in your Python projects. Check out my online courses. Follow me on Twitter. ⭐️



Học mã miễn phí. Chương trình giảng dạy nguồn mở của Freecodecamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu

Sự khác biệt giữa phụ lục và mở rộng Python) là gì? Giải thích với ví dụ?

append () thêm một phần tử vào cuối danh sách trong khi .extend () có thể thêm nhiều phần tử riêng lẻ vào cuối danh sách. extend() can add multiple individual elements to the end of the list.

Mở rộng trong Python với ví dụ là gì?

extend () là chức năng tích hợp của Python. Nó lặp đi lặp lại so với các yếu tố được chỉ định và nối các yếu tố của nó vào cuối danh sách hiện tại.Danh sách.Phương thức mở rộng () tương đương với danh sách [LEN (Danh sách):] = Itable (cộng đồng các phần tử của điều đó sau phần tử cuối cùng của danh sách).Python's built-in function. It iterates over the specified iterable and appends its elements to the end of the current list. The list. extend() method is equivalent to list[len(list):] = iterable (appends the elements of the iterable after the list's last element).

Phương thức append () giải thích với ví dụ là gì?

Phương thức Python append () thêm một mục vào cuối danh sách.Nó nối thêm một yếu tố bằng cách sửa đổi danh sách.Phương pháp không trả lại chính nó.Mục này cũng có thể là một danh sách hoặc từ điển tạo ra một danh sách lồng nhau.Phương pháp được mô tả dưới đây.adds an item to the end of the list. It appends an element by modifying the list. The method does not return itself. The item can also be a list or dictionary which makes a nested list. Method is described below.

Sự khác biệt giữa phần mở rộng và chèn là gì?

Hàm append () có độ phức tạp thời gian không đổi của O (1).Hàm chèn () có độ phức tạp tuyến tính của O (n).Hàm mở rộng () có độ phức tạp về thời gian của O (k), trong đó "K" là chiều dài của điều đó.