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 write a slide puzzle game with Python and Pygame (2020 tutorial)

How to create a memory puzzle game with Python and Pygame (#005)

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