What's the best and fastest way to access a value from the previous iteration in a for loop, assuming that the object will be very large (example, a cursor object which has 100,000+ records)
Using a simple example:
tmp = [
['xyz', 335], ['zzz', 338], ['yyy', 339], ['yyy', 442],
['abc', 443], ['efg', 444], ['ttt', 446], ['fff', 447]
]
for x in tmp:
if not prev:
prev = x[1]
print 'seq: ', x[1], 'prev seq:', prev, 'variance: ', x[1]-prev
prev = x[1]
Is this the most optimal way to handle this?
Based on the responses below i did some testing: tmp was created with 500 lists, the average of running it 20 times is shown below.
results:
Mines: 0,623
Dave snippet1: 0,605
Dave snippet2: 0,586
Catchmeifyoutry (edited code): 0,707