# [Tutor] Looking for a Pythonic way to pass variable

Bill Mill bill.mill at gmail.com
Tue Mar 22 21:27:02 CET 2005

```On Tue, 22 Mar 2005 13:30:09 -0500, Kent Johnson <kent37 at tds.net> wrote:
> C Smith wrote:
> >
> > On Tuesday, Mar 22, 2005, at 05:01 America/Chicago,
> > tutor-request at python.org 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
```