440,676 Members | 2,262 Online
Need help? Post your question and get tips & solutions from a community of 440,676 IT Pros & Developers. It's quick & easy.

# Looping over lists

 P: n/a I have a list: a = [1., 2., 3., 4., 5.] I want to loop over a and then loop over the elements in a that is to the right of the current element of the first loop In C this would be equivalent to: for(i = 0; i < n; i++) { for(j=i+1; j < n; j++) { print a[i], a[j] and should yield: 1. 2. 1. 3. 1. 4. 1. 5. 2. 3. 2. 4. 2. 5. 3. 4. 3. 5. 4. 5. Can anyone help me with the right approach for this in python? Cheers Tommy May 5 '07 #1
8 Replies

 P: n/a Tommy Grav

 P: n/a Tommy Grav wrote: I have a list: a = [1., 2., 3., 4., 5.] I want to loop over a and then loop over the elements in a that is to the right of the current element of the first loop In C this would be equivalent to: for(i = 0; i < n; i++) { for(j=i+1; j < n; j++) { print a[i], a[j] and should yield: 1. 2. 1. 3. 1. 4. 1. 5. 2. 3. 2. 4. 2. 5. 3. 4. 3. 5. 4. 5. Can anyone help me with the right approach for this in python? Two more options: def pop_combine(items): items = list(items) while items: a = items.pop(0) for b in items: print a, b def enumerate_combine(items): for i, a in enumerate(items): for b in items[i+1:]: print a, b Peter May 5 '07 #3

 P: n/a Tommy Grav

 P: n/a I think the for i in range() is more readable (Maybe because I'm coming from a c-style syntax language background) - but what would the benefits of using enumerate be (other that being more . . . pythonesque?) On 5/5/07, Dennis Lee Bieber a = [1, 2, 3, 4, 5, 3, 6] #just to confuse mattersfor pos, val in enumerate(a): ... for v2 in a[pos+1:]: ... print val, v2 ... 1 2 1 3 1 4 1 5 1 3 1 6 2 3 2 4 2 5 2 3 2 6 3 4 3 5 3 3 3 6 4 5 4 3 4 6 5 3 5 6 3 6 >> -- Wulfraed Dennis Lee Bieber KD6MOG wl*****@ix.netcom.com wu******@bestiaria.com HTTP://wlfraed.home.netcom.com/ (Bestiaria Support Staff: we******@bestiaria.com) HTTP://www.bestiaria.com/ -- http://mail.python.org/mailman/listinfo/python-list May 5 '07 #5

 P: n/a On May 5, 3:15 am, kaens >a = [1, 2, 3, 4, 5, 3, 6] #just to confuse matters >>for pos, val in enumerate(a): ... for v2 in a[pos+1:]: ... print val, v2 ... 1 2 1 3 1 4 1 5 1 3 1 6 2 3 2 4 2 5 2 3 2 6 3 4 3 5 3 3 3 6 4 5 4 3 4 6 5 3 5 6 3 6 -- Wulfraed Dennis Lee Bieber KD6MOG wlfr...@ix.netcom.com wulfr...@bestiaria.com HTTP://wlfraed.home.netcom.com/ (Bestiaria Support Staff: web-a...@bestiaria.com) HTTP://www.bestiaria.com/ -- http://mail.python.org/mailman/listinfo/python-list May 5 '07 #6

 P: n/a Dustan

 P: n/a Dustan a = [1, 2, 3, 4, 5, 3, 6] #just to confuse matters >for pos, val in enumerate(a): ... for v2 in a[pos+1:]: ... print val, v2 This "breaks symmetry", by using enumerate in the outer loop and a slice in the inner loop; the symmetrical construction of using range in both loops is a big conceptual/clarity win -- the reader of the code needs to "grasp" one fewer concept (locally). Using xrange in both loops would be just as good from this POV. Alex May 5 '07 #8

 P: n/a En Sat, 05 May 2007 05:15:32 -0300, kaens escribió: I think the for i in range() is more readable (Maybe because I'm coming from a c-style syntax language background) - but what would the benefits of using enumerate be (other that being more . . . pythonesque?) If you want to iterate over a sequence and process each item, you could do this (as one would do in C): for i in range(len(values)): dosomethingwith(values[i]) Problems: values[i] gets translated into values.__getitem__(i), and that requires a lookup for the __getitem__ method and a function call; and you don't actually need the index i, but it's created anyway. In this case the best (and fastest) way in Python is: for item in values: dosomethingwith(item) No spurious variable, no method lookup, no aditional function call. But what if you need *also* the index? On earlier Python versions one had to go back to the first method; enumerate() was made just for this case: for i, item in enumerate(values): dosomethingwith(item, i) You get all the advantages of the iterator approach plus the index available. -- Gabriel Genellina May 6 '07 #9

### This discussion thread is closed

Replies have been disabled for this discussion.