Solved

Looping problem - Python 3.4

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
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 modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

932 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

11 Experts available now in Live!

Get 1:1 Help Now