[Tutor] Looking for a Pythonic way to pass variable
Kent Johnson
kent37 at tds.net
Tue Mar 22 19:30:09 CET 2005
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.
Kent
More information about the Tutor
mailing list