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

Rick Johnson rantingrickjohnson at gmail.com
Mon Jul 17 22:04:22 EDT 2017


On Monday, July 17, 2017 at 3:10:51 PM UTC-5, aaron.m.... 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?

Solving problems in the "programming realm" is not much
unlike solving problems in real life. First, image you had 8
apples laying on the floor. Now imagine the steps required
to collect the apples into "sacks of two".

    (1) First, grab one large sack and enough small sacks to
    hold all the apples. 8 / 2.0 = (4.0 small sacks)
        
    (2) Then, position yourself at one end of the "row of apples".
        
    (3) Now grab two apples and put them both into a small sack.
    Then, put the small sack into the large sack.
        
    (4) Repeat step 3 until there are no more apples on the
    floor.
    
There you go. All you have to do now is translate that into
Python code. Shouldn't be too difficult. And your professor
will be so proud ;-)




More information about the Python-list mailing list