The difference of list methods between append() and extend() in Python
Python lists are mutable, you can add objects to the list in place. Both append() and extend() methods could do the trick.
append(): add a single object to the end of the list
>>> l = ['a', 'b', 'c']
>>> l.append(['d','e','f'])
>>> l
['a', 'b', 'c', ['d', 'e', 'f']]
extend(): add all the elements from the iterable to the end of the list
>>> l.extend(['d','e','f'])
>>> l
['a', 'b', 'c', 'd', 'e', 'f']
Comments
Post a Comment