# [Tutor] Problems with iterations and breaking loops

Alan Gauld alan.gauld at btinternet.com
Thu Mar 18 09:50:54 CET 2010

```"Karjer Jdfjdf" <karper12345 at yahoo.com> wrote

> In reality I have to do this for a much larger dataset
> (eg 10000 range * 100000 = a lot), so the for v1, v2 in
> list_of_tuples doesn't work because it is too large for it.

What does the size have to do with it?
v1,v2 in list_of_tuples will only unpack one tuple at a
time regardless of how big the list is.

> I've tried to put it in a def, but:

Take the outer loop out of the function.
The point of functions is to break the problem down into
manageable chunks.

You should probably have 3 functions here:
1) to do the calculation on a single element
2) to apply function(1) to each item in the tuple list
3) a function to apply function(") from min to max

That makes your code look like

def function3(min,max)
for n in range(min,max):
function2(val, t_lst)

def function2((val, tuples)
for tuple in tuples:
function3(val,tuple)

def function(val,pair)
# whatever calc you need

That way you can test each function in turn and
stop the nested loops from dominating the logic.

def perform_calculations2(n, nmax, value, list_of_tuples, ):
#this works too good. It returns 10 times the correct 30 values

Because you have repeated the outer loop inside the function.
Get rid of it.

something = []
for i2 in xrange(n, int(nmax+1)):

for t in list_of_tuples:
v1 = t[0]
v2 = t[1]
something.append(str(i2) + "\tdoes something for\t" + str(v2))
return something

The above is all you need in your function.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

```