The difference between the extend() and append() of the python operation array_20210125

The difference between append() and extend() in list

  • (1) L.append(object) -> None – append object to end

    append(object) is to add an object to the list as a whole, the added list has one more element than the original list, the parameter of this function can be any type of object, the function has no return value

  • (2) L.extend(iterable) -> None – extend list by appending elements
    from the iterab

    extend(iterable) is to add each element in an iterable object to the list one by one. There are several elements in the iterable object. The added list will have a few more elements than the original list. The parameter of this function must be Iterable object, the function has no return value

example:

>>> a = [1,2,3]
>>> b = [4,5,6]
>>> c = [7,8,9]
>>> a.extend(c)
>>> a
[1, 2, 3, 7, 8, 9]
>>> b.append(c)
>>> b
[4, 5, 6, [7, 8, 9]]
>>>

Guess you like

Origin blog.csdn.net/a18829292719/article/details/113131287