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

Rahul K P 36rahu at gmail.com
Mon Jul 17 16:27:16 EDT 2017


You can use a simple logic and list comprehension.
so it will be like this
lst = [1, 2, 3, 4, 5, 6, 7, 8]
print [lst[i:i+2] for i in range(0,len(lst),2)]
Here 2 is the pairing number, You can set is as your need.
On Tue, Jul 18, 2017 at 1:40 AM, <aaron.m.weisberg at gmail.com> wrote:
> 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
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
Regards
*Rahul K P*
Python Developer
Mumbai
+919895980223


More information about the Python-list mailing list

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