Quickly distinguish between append() and extend() functions

1. append() function


1.0 Features:

append() is a built-in function in Python that appends data to the end of the list.

1.1 Features:

Only one data can be appended at a time, and the data can be of any type, including numeric values ​​(integers, floating point types), strings, tuples, lists, sets, and dictionaries.

Example 1:

list1 = ["张三", "李四", "王五"]
list1.append("赵六")
print("追加字符串的结果是:", list1)
list1.append(123)
print("追加数值的结果是:", list1)

The running results are as follows:

The result of appending the string is: [‘Zhang San’, ‘Li Si’, ‘Wang Wu’, ‘Zhao Liu’]

The result of appending the value is: [‘Zhang San’, ‘Li Si’, ‘Wang Wu’, ‘Zhao Liu’, 123]

1.2 Notes:

If the appended data is a tuple, list, set, or dictionary, this function will append them to the original list as a whole.

Example 2:

list1 = ["张三", "李四", "王五"]
list1.append(("赵六", "pig"))
print("追加元组的结果是:", list1)
list1.append({
    
    456, "pig"})
print("追加集合的结果是:", list1)

The running results are as follows:

The result of appending tuples is: [‘Zhang San’, ‘Li Si’, ‘Wang Wu’, (‘Zhao Liu’, ‘pig’)]

The result of appending the set is: [‘Zhang San’, ‘Li Si’, ‘Wang Wu’, (‘Zhao Liu’, ‘pig’), {456, ‘pig’}]


2. extend() function


2.0 Features:

extend() is a built-in function in Python that appends data to the end of the list.

2.1 Features:

Only one data can be appended at a time. The data must be an "iterable" object, such as: string, tuple, list, set, dictionary. This function will add the elements in the iterable object to the original list one by one. The extend function cannot be used for numerical values ​​(integer, floating point).

Example 3:

list1 = ["张三", "李四", "王五"]
list1.extend(["赵六", "520", "dog"])
print("追加列表的结果是:", list1)
list1 = ["张三", "李四", "王五"]
list1.extend({
    
    "赵六", "520", "dog"})     # 集合是无序的,追加的数据也是无序添加的
print("追加集合的结果是:", list1)

The running results are as follows:

The result of appending the list is: [‘Zhang San’, ‘Li Si’, ‘Wang Wu’, ‘Zhao Liu’, ‘520’, ‘dog’]

The result of the additional set is: [‘Zhang San’, ‘Li Si’, ‘Wang Wu’, ‘520’, ‘dog’, ‘Zhao Liu’]

2.2 Notes:

If the appended object is a string, this function will split each element in the string and add it to the original list one by one.

Example 4:

list1 = ["张三", "李四", "王五"]
list1.extend("赵六")
print("追加字符串的结果", list1)
list1 = ["张三", "李四", "王五"]
list1.extend("520")
print("追加字符串的结果", list1)

The running results are as follows:

The result of appending string [‘Zhang San’, ‘Li Si’, ‘Wang Wu’, ‘Zhao’, ‘Liu’]

The result of appending string [‘Zhang San’, ‘Li Si’, ‘Wang Wu’, ‘5’, ‘2’, ‘0’]

2.3 Special instructions:

If the appended object is a dictionary, extend will add the "key" of the dictionary by default. If you only want to add the "value" or "key-value pair", you can use the dictionary's .values() and .items () method .

Example 5:

list1 = ["张三", "李四", "王五"]
list1.extend({
    
    "赵六": "pig", "hao": [1,2,3]})    # 此处用.keys()效果同默认效果
print("追加字典的默认结果是", list1)

The running results are as follows:

The default result of appending the dictionary is [‘Zhang San’, ‘Li Si’, ‘Wang Wu’, ‘Zhao Liu’, ‘hao’]

list1 = ["张三", "李四", "王五"]
list1.extend({
    
    "赵六": "pig", "hao": [1,2,3]}.values())
print("追加字典的values结果是", list1)

The running results are as follows:

The result of appending the dictionary values ​​is [‘Zhang San’, ‘Li Si’, ‘Wang Wu’, ‘pig’, [1, 2, 3]]

list1 = ["张三", "李四", "王五"]
list1.extend({
    
    "赵六": "pig", "hao": [1,2,3]}.items())    # items()是将键值对以元组的形式存放
print("追加字典的items结果是", list1)

The running results are as follows:

The result of appending dictionary items is [‘Zhang San’, ‘Li Si’, ‘Wang Wu’, (‘Zhao Liu’, ‘pig’), (‘hao’, [1, 2, 3])]

*The following is the result of using the append function, which can be compared with the above result.

The result of appending the dictionary is [‘Zhang San’, ‘Li Si’, ‘Wang Wu’, {‘Zhao Liu’: ‘pig’, ‘hao’: [1, 2, 3]}]


Summarize:

  • append() and extend() are both used to append data at the end of the list, and both areOnly one data object can be added at a time, both can be addedStrings, tuples, lists, sets and dictionaries, the difference lies in the different effects after adding data;
  • If the appended data is of numeric type, only the append() function can be used;
  • If the appended data isa single string, it is recommended to use the append() function. If you have special needs, you can also use extend() ;
  • If the appended data istuple, list, or set, it needs to be added as a whole, select append, and to add elements one by one, select extend;
  • If the appended data is a dictionary, append will be added as a dictionary as a whole, while extend will add the dictionary keys one by one by default, if necessary To add values ​​or key-value pairs, you can use the dictionary's .values() and .items() methods.

Guess you like

Origin blog.csdn.net/weixin_43498642/article/details/129449781