Solved

Looping problem - Python 3.4

Posted on 2014-11-27
2
133 Views
Last Modified: 2014-11-27
I am trying to create a two dimensional list (newlist) from a single dimensional list (Original).

Original =[6,12,45,23,8,32,99,100]

newlist =[[6, 12], [45, 23], [8, 32],[99,100]]  ..... desired result

newlist =[[6, 12], [45, 23], [8, 32]] ............ actual result

Looks like the looping stops before the end of the process.

I have attached the .PY file

Thanks in advance for any help.
testing.py
0
Comment
Question by:jameskane
2 Comments
 
LVL 25

Accepted Solution

by:
clockwatcher earned 500 total points
ID: 40469348
In general, it's rarely a good idea to change the control variable that you're iterating over.  Here's one way to get the results that you're after.  
while len(Original) != 0:
   newlist.append(Original[0:2])
   Original = Original[2:]

Open in new window

0
 

Author Closing Comment

by:jameskane
ID: 40469394
Perfect. AND I learned something important

Many thanks
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now