简体   繁体   中英

Append value to each sublist in a list

I have a list of list that I want to append a constant value to each sublist of the full list, for instance:

_lst = [[1, 2], [3, 4], [5, 6]]

and I want to append 7 to each of the sublist so that _lst becomes:

[[1, 2, 7], [3, 4, 7], [5, 6, 7]]

Is there a good way to complete the job (such as using zip )? Thanks!

for l in _lst:
    l.append(7)
_lst = [ele + [7] for ele in _lst]
>>> tmp = [ i.append(7) for i in _lst ]
>>> print _lst
[[1, 2, 7], [3, 4, 7], [5, 6, 7]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM