Combining every pair of list items and creating a new list.

ast nomail at com.invalid
Tue Jul 18 10:23:38 EDT 2017


<aaron.m.weisberg at gmail.com> a écrit dans le message de 
news:621ca9d5-79b1-44c9-b534-3ad1b0cf44a4 at googlegroups.com...
> Hi,
>> I'm having difficulty thinking about how to do this as a Python beginner.
>> But I have a list that is represented as:
>> [1,2,3,4,5,6,7,8]
>> and I would like the following results:
>> [1,2] [3,4] [5,6] [7,8]
>> Any ideas?
>> Thanks

list(zip(L[0::2], L[1::2]))
[(1, 2), (3, 4), (5, 6), (7, 8)]
>>> list(map(list, zip(L[0::2], L[1::2])))
[[1, 2], [3, 4], [5, 6], [7, 8]]


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /