python - append a list into end of list in list -
is there way 'merge' 2 lists item in 1 list can appended end of list in list? example...
a2dlist=[['a','1','2','3','4'],['b','5','6','7','8'],[........]] otherlist = [9,8,7,6,5] thefinallist=[['a','1','2','3','4',9],['b','5','6','7','8',8],[....]] i'm not sure if matter a2dlist made of strings , otherlist numbers... i've tried append end
thefinallist=[['a','1','2','3','4'],['b','5','6','7','8'],[.......],[9,8,7,6,5]
>>> a2dlist=[['a','1','2','3','4'],['b','5','6','7','8']] >>> otherlist = [9,8,7,6,5] >>> x, y in zip(a2dlist, otherlist): x.append(y) >>> a2dlist [['a', '1', '2', '3', '4', 9], ['b', '5', '6', '7', '8', 8]] on python 2.x consider using itertools.izip instead lazy zipping:
from itertools import izip # returns iterator instead of list also note zip automatically stop upon reaching end of shortest iterable, if otherlist or a2dlist had 1 item, solution work without error, modifying lists index risks these potential problems.
Comments
Post a Comment