What's the difference between the list methods append()
and extend()
?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
append
adds an element to a list, andextend
concatenates the first list with another list (or another iterable, not necessarily a list.)From Dive into Python.
append(): It is basically used in Python to add one element.
extend(): Where extend(), is used to merge two lists or insert multiple elements in one list.
append
"extends" the list (in place) by only one item, the single object passed (as argument).extend
"extends" the list (in place) by as many items as the object passed (as argument) contains.This may be slightly confusing for
str
objects.append
will add a single string item at the end butextend
will add as many "single" 'str' items as the length of that string.append
will still add a single 'list' item at the end andextend
will add as many 'list' items as the length of the passed list.produces:
Append and extend are one of the extensibility mechanisms in python.
Append: Adds an element to the end of the list.
To add a new element to the list, we can use append method in the following way.
The default location that the new element will be added is always in the (length+1) position.
Insert: The insert method was used to overcome the limitations of append. With insert, we can explicitly define the exact position we want our new element to be inserted at.
Method descriptor of insert(index, object). It takes two arguments, first being the index we want to insert our element and second the element itself.
Extend: This is very useful when we want to join two or more lists into a single list. Without extend, if we want to join two lists, the resulting object will contain a list of lists.
If we try to access the element at pos 2, we get a list ([3]), instead of the element. To join two lists, we'll have to use append.
To join multiple lists
With append you can append a single element that will extend the list:
If you want to extend more than one element you should use extend, because you can only append one elment or one list of element:
So that you get a nested list
Instead with extend you can extend a single element like this
Or, differently from append, extend more elements in one time without nesting the list into the original one (that's the reason of the name extend)
Adding one element with both methods
append 1 element
extend one element
If you use append for more than one element, you have to pass a list of elements as arguments and you will obtain a NESTED list!
With extend, instead, you pass a list as argument, but you will obtain a list with the new element that are not nested in the old one.
So, with more elements, you will use extend to get a list with more items. You will use append, to append not more elements to the list, but one element that is a nested list as you can clearly see in the output of the code.
append
: Appends object at the end.gives you:
[1, 2, 3, [4, 5]]
extend
: Extends list by appending elements from the iterable.gives you:
[1, 2, 3, 4, 5]