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

MRAB python at mrabarnett.plus.com
Mon Jul 17 16:42:18 EDT 2017


On 2017-07-17 21:10, 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
> 
Those are slices of the original list. You can do it using a 'for' loop 
over a 'range' with a step/stride of 2 and getting slices of the 
original list.



More information about the Python-list mailing list