Solved

Looping problem - Python 3.4

Posted on 2014-11-27
2
141 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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. …
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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 …

776 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