Detailed explanation of python’s extend function


in Python, extend() is a built-in method of the list type that adds a list (or any iterable element) to the end of the current list. Below we will explain in detail how this function works and how to use it.

grammar

list.extend(iterable)
  • listis the list you want to extend.
  • iterableCan be any iterable object such as list, tuple, set, dictionary (only keys are added), etc.

Function

extend()The method adds each element of iterable to the end of list. This essentially "extends" all elements of iterable into list.

Example

Here are some examples of using theextend() method:

Example 1: Add a list

# 创建一个列表
list1 = [1, 2, 3]

# 创建另一个列表
list2 = [4, 5, 6]

# 使用 extend() 方法将 list2 扩展到 list1
list1.extend(list2)

print(list1)  # 输出: [1, 2, 3, 4, 5, 6]

Example 2: Add tuple

# 创建一个列表
list1 = [1, 2, 3]

# 创建一个元组
tuple1 = (4, 5, 6)

# 使用 extend() 方法将 tuple1 扩展到 list1
list1.extend(tuple1)

print(list1)  # 输出: [1, 2, 3, 4, 5, 6]

Example 3: Add a collection

# 创建一个列表
list1 = [1, 2, 3]

# 创建一个集合
set1 = {
    
    4, 5, 6}

# 使用 extend() 方法将 set1 扩展到 list1
list1.extend(set1)

print(list1)  # 输出可能是 [1, 2, 3, 4, 5, 6],但集合是无序的,所以顺序可能会有所不同。

Example 4: Add dictionary (add keys only)

# 创建一个列表
list1 = [1, 2, 3]

# 创建一个字典
dict1 = {
    
    'a': 10, 'b': 20, 'c': 30}

# 使用 extend() 方法将 dict1 的键扩展到 list1
list1.extend(dict1)

print(list1)  # 输出: [1, 2, 3, 'a', 'b', 'c']

Of course, here are more detailed examples of the Python extend() method:

Example 5: Add string

# 创建一个列表
list1 = [1, 2, 3]

# 创建一个字符串
str1 = 'abc'

# 使用 extend() 方法将 str1 扩展到 list1
list1.extend(str1)

print(list1)  # 输出: [1, 2, 3, 'a', 'b', 'c']

Insert image description here

Example 6: Mixed type extension

# 创建一个列表
list1 = [1, 2, 3]

# 创建一个包含不同类型元素的列表
list2 = [4, 'a', (5, 6), {
    
    'key': 'value'}]

# 使用 extend() 方法将 list2 扩展到 list1
list1.extend(list2)

print(list1)  # 输出: [1, 2, 3, 4, 'a', (5, 6), {'key': 'value'}]

Insert image description here

Example 7: Extending an empty list or non-iterable object

# 创建一个列表
list1 = [1, 2, 3]

# 尝试扩展一个空列表
empty_list = []
list1.extend(empty_list)
print(list1)  # 输出: [1, 2, 3],空列表不会影响原列表

# 尝试扩展一个不可迭代的对象,例如整数
try:
    list1.extend(5)  # 这会引发 TypeError,因为整数是不可迭代的。
except TypeError as e:
    print(e)  # 输出: 'int' object is not iterable

Insert image description here

Precautions:

  • extend()The method has no return value, it directly modifies the original list. This means that it expands the list in place rather than creating a new list.

Comparison of extend function, append function, + and += functions

extend(), append(), + and += can all be used to add elements to a list in Python, but they There are some important differences between them.

1. extend()

  • Function: Adds all elements of an iterable object to the end of the list.
  • Whether the original list has changed: Yes, modify the original list directly.
  • Reply: No.
  • Example:
list1 = [1, 2, 3]
list1.extend([4, 5])
print(list1)  # 输出: [1, 2, 3, 4, 5]

2. append()

  • Function: Adds an element to the end of the list.
  • Whether the original list has changed: Yes, modify the original list directly.
  • Reply: No.
  • Example:
list1 = [1, 2, 3]
list1.append(4)
print(list1)  # 输出: [1, 2, 3, 4]

3. +

  • Function: Join two lists to create a new list.
  • Whether the original list has changed: No, the original list is not modified.
  • Return value: A new list containing all elements of the two original lists.
  • Example:
list1 = [1, 2, 3]
list2 = [4, 5]
new_list = list1 + list2
print(new_list)  # 输出: [1, 2, 3, 4, 5]

4. +=

  • Function: Add all elements of an iterable object to the end of the list, similar to extend(), but can also be used for other data Type (e.g. string).
  • Whether the original list has changed: Yes, modify the original list directly.
  • Reply: No.
  • Example:
list1 = [1, 2, 3]
list1 += [4, 5]
print(list1)  # 输出: [1, 2, 3, 4, 5]

Summarize:

extend()Python's method is a very useful tool that allows you to add all elements of an iterable object to the end of a list. With the example above, you can see how extend() works, and some things to note when using it. Remember, extend() directly modifies the original list, rather than creating a new one.

  • If you want to add all elements of an iterable to the end of the list and don't mind modifying the original list directly, then extend() or += is a good choice. Among them, extend() more clearly indicates that you are adding multiple elements.
  • If you just want to add one element to the end of the list and don't mind modifying the original list directly, use append().
  • If you want to concatenate two lists without modifying either of the original lists, then using + is the best choice because it creates a new list.

Insert image description here

Guess you like

Origin blog.csdn.net/hhhhhhhhhhwwwwwwwwww/article/details/134868737