On Tue, 22 Mar 2005 13:30:09 -0500, Kent Johnson <[EMAIL PROTECTED]> wrote: > C Smith wrote: > > > > On Tuesday, Mar 22, 2005, at 05:01 America/Chicago, > > [EMAIL PROTECTED] wrote: > > > >> I met a similar question. > >> what if one has L = [[1,2],[3,4]], K = [100, 200] > >> How to 'zip' a List like [[1,2,100], [3,4,200]]? > >> > > I would do something like: > > > > ### > > for i in range(len(L)): > > L[i].append(K[i]) > > ### > > Oh, the light goes on :-) Thanks C Smith! > > Here is another way: > >>> L = [[1,2],[3,4]] > >>> K = [100, 200] > >>> [ x+[y] for x, y in zip(L, K) ] > [[1, 2, 100], [3, 4, 200]] > > Note C Smith's approach modifies L to include the items in K; my approach > makes a new list.
Just for kicks - if you don't mind if the 100 and 200 appear first instead of last, and conversion of your inner lists to tuples, then: >>> L = [[1,2], [3,4]] >>> K = [100, 200] >>> zip(K, *L) [(100, 1, 3), (200, 2, 4)] works, and looks a little nicer. Also, to modify the list in-place with a listcomp, you could use: >>> L = [[1,2], [3,4]] >>> K = [100, 200] >>> [x.append(y) for x, y in zip(L, K)] [None, None] >>> L [[1, 2, 100], [3, 4, 200]] And, to create a new list in the format you originally asked for, we can modify the first trick I showed you: >>> L = [[1,2], [3,4]] >>> K = [100, 200] >>> [[b,c,a] for a,b,c in zip(K, *L)] [[1, 3, 100], [2, 4, 200]] which I think is pretty cool, if a little obtuse. Peace Bill Mill bill.mill at gmail.com _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor