473,586 Members | 2,718 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1567
ma***********@h otmail.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
8292
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
2549
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 it makes however I would like the entire script to loop 34 times and on each of those 30 loops also write the product and the price (only) onto the...
13
14717
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
1747
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 events do this for me, but my extended textbox doesn't get created by those wonderful form setup wizards. So, 1) Is there a way I can pick up these...
13
1486
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
3769
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 doesn't move to next element... this sad thing but, it's indication that we must move to try catch instead of on error.
0
24162
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 Bound to the Upper Bound of the Array. For Each...Next seems simpler because you need not worry about retrieving the Lower and Upper Bounds- the...
11
17162
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
3
1722
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 code goes something like this: for i = 1 to 5 'this loop calls each array for j = 0 to 4 ' this loop calls each element if array (i,j)...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8200
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7954
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8215
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3836
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.