Approaches
Native Python (@acushner presented this first):
lsts = [[1,345,0,304], [0,345,678,946,90], [0,23,3,56,3,5,9,0]]
[lst + [90] for lst in lsts]
Alternatively, with itertools.chain
:
import itertools as it
[list(it.chain(lst, [90])) for lst in lsts]
For fun, a third-party library more-itertools
(pip install more_itertools
):
import more_itertools as mit
[list(mit.padded(lst, fillvalue=90, n=len(lst)+1)) for lst in lsts]
Caveat
Some answers attempt to mutate a list while iterating. While those options give equivalent results, are possibly more memory efficient and may even be faster for larger data for this specific problem, they are arguably non-pythonic and not recommended practices.
From the Python docs:
It is sometimes tempting to change a list while you are looping over it; however, it is often simpler and safer to create a new list instead.
This is especially true when removing or inserting elements from a list while iterating it. The latter approaches adopt this convention of creating a new list. However, for certain innocuous circumstances, a compromise may be iterating over a copy of the nested list:
lists = [[1,345,0,304], [0,345,678,946,90], [0,23,3,56,3,5,9,0]]
for lst in lists[:]:
lst.append(90)
lists
Otherwise, default to @acushner's approach, which is the next performant option discussed here.