In python, the difference between append () of list type and extend () is: pythonappend
The list types in python are commonly used: append () function and extend () function. The two functions are similar. What is the difference between them?
1. append ()
Append () usage example: >>> mylist = [1, 2, 0, 'abc'] >>> mylist [1, 2, 0, 'abc'] >>> mylist. append (4) >>> mylist [1, 2, 0, 'abc', 4] >>> mylist. append ('hahaha') >>> mylist [1, 2, 0, 'abc', 4, 'hahaha']
2. extend ()
Extend () usage example: >>> mylist [1, 2, 0, 'abc', 4, 'hahaha'] >>> mylist. extend (['lulu']) >>> mylist [1, 2, 0, 'abc', 4, 'haha', 'lulu'] >>> mylist. extend ([aaa, 'lalala ']) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'aaa' is not defined >>> mylist. extend (['123', 'lalala '])> mylist [1, 2, 0, 'abc', 4, 'hahaha', 'lulu ', '200', 'lalala '] >>> mylist. extend ([111111,222]) >>> mylist [1, 2, 0, 'abc', 4, 'hahaha', 'lulu', '123', 'lalala ', 111111,222]
Analysis shows that:
Append () can only add one element to the list;
Extend () can only add another list.