Appending item to lists within a list comprehension

list.append mutates the list itself and returns None. List comprehensions are for storing the result, which isn’t what you want in this case if you want to just change the original lists.

>>> x = [[1, 2], [3, 4], [5, 6]]
>>> for sublist in x:
...     sublist.append('a')
...
>>> x
[[1, 2, 'a'], [3, 4, 'a'], [5, 6, 'a']]

Leave a Comment