473,383 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Looking at the next element in a for loop

-
Hi,

If I have a simple for loop like this:

for a in b:
print a

Is there a way I can get the next element in the loop? Something like this:

for a in b:
if a == 1:
print <next a>
Jul 18 '05 #1
2 1553
ma***********@hotmail.com (-) writes:
Is there a way I can get the next element in the loop? Something like this:

for a in b:
if a == 1:
print <next a>


No, not as long as you use an iterator. If you use an index instead,
it's easy of course.

for i in xrange(len(b)):
if b[i] is 1:
print b[i+1]
Jul 18 '05 #2
- wrote:
If I have a simple for loop like this:

for a in b:
print a

Is there a way I can get the next element in the loop? Something like
this:

for a in b:
if a == 1:
print <next a>


You could reverse the logic:
wasMatch = False
for a in [1,2,1,1,1,3,1,4]: .... if wasMatch: print a,
.... wasMatch = a == 1
....
2 1 1 3 4
Or do something like this:
b = [1,2,1,1,1,3,1,4]
for a, nexta in zip(b, b[1:]): .... if a == 1: print nexta,
....
2 1 1 3 4


If you like the latter, you can use the window() function on the itertools
example page http://www.python.org/doc/current/li...s-example.html
for a better implementation:

for a, nexta in window(b):
if a == 1: print nexta,

Peter

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Brian Burgess | last post by:
Hi all, Is there any 'trick' anyone knows to control a For Next to pass to the next iteration in ASP VBScript? ..Similar to the 'continue' keyword of C and C++? Thanks in advance.. -BB
0
by: Kingdom | last post by:
I Need some serious help here. strugling novis with ASP and javascript any help would be greatly appreciated The script below does exactly what I want it to do for each product on the two passes...
13
by: Joseph Garvin | last post by:
When I first came to Python I did a lot of C style loops like this: for i in range(len(myarray)): print myarray Obviously the more pythonic way is: for i in my array: print i
7
by: Bruce HS | last post by:
I'd like to call my ancestor Validation Function every time any control on a Win Form generates a Validating or Validated event. I'm using VB. I've extended Textbox, for instance, to have its...
13
by: andreas | last post by:
Hi, I want to do some calculation like ( t1 and t2 are known) for i = t1 to t2 for j = t1 to t2 ..... ..... for p = t1 to t2 for q = t1 to t2
4
by: Neo | last post by:
I found on error resume next doesn't work in for each... e.g. on error resume next for each x in y 'do stuff next if you have an error in for each loop, it falls in infinite loop... it...
0
ADezii
by: ADezii | last post by:
If you want to visit each item in an Array, you have two alternatives: Use a For Each..Next loop, using a Variant to retrieve each value in turn. Use a For...Next loop, looping from the Lower...
11
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
3
by: smileyc | last post by:
I have 5 arrays, named array1 array2 array3 array4 array5. They are integer arrays each with 5 elements in them. I want to access all the elements in all the arrays using a for next loop,the pseudo...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.