Reprint: http://justjavac.iteye.com/blog/1827915
1>>> Li = ['a','b','C'] 2>>> Li.extend (['D','e','F']) 3>>>Li4['a','b','C','D','e','F'] 5>>>Len (LI)667>>> li[-1] 8 'F' 9>>> Li = ['a','b','C'] Ten>>> Li.append (['D','e','F']) One>>>Li A['a','b','C', ['D','e','F']] ->>>Len (LI) -4 the>>> li[-1] -['D','e','F']
- Lists's two methods extend and append look similar, but in fact they are completely different. Extend accepts a parameter, which is always a list, and adds each element in the list to the original list.
- There are 3 elements (' A ', ' B ' and ' C ') in the list, and a second list with 3 elements (' d ', ' e ' and ' F ') is expanded, so there are 6 elements in the new list.
- On the other hand, append accepts a parameter, which can be any data type, and is simply appended to the tail of the list. Call the Append method here using a list parameter containing 3 elements.
- The list that originally contains 3 elements now contains 4 elements. Why is it 4 elements? Because the last element you just appended is a list. The list can contain any type of data, including other lists. This may be the result you want, maybe not. If your intent is extend, do not use append.
Difference between python:extend (extension) and append (append)