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

Popular posts from this blog

How to use Python conditional expression?(Python Conditional Expression)

How to check if a string contains a substring in Python

Introduction to multitasking with Python #001 multithreading (2020 tutorial)