3

I have three different loops in my code but I am wondering if there is a way to combine two of them where it only loops through once. My code is below:

for x in groups: A bunch of code that produces the following... frames[x] highcs[x] 

I then loop through two loops below. These are the ones I'm trying to combine.

for x, frame in frames.items(): more unrelated code for x, highc in highcs.items(): 

The final two loops are really just creating excel workbooks using xlsxwriter. The first loop creates the workbook and inputs dataframes from frames[x] onto the worksheets. Then highc just goes back over the same sheets and adds new dataframes.

5
  • Are frames and highcs expected to be of the same length? Commented Apr 20, 2016 at 16:11
  • are frames and highcs the same length? Commented Apr 20, 2016 at 16:12
  • yes, they are the same length Commented Apr 20, 2016 at 16:13
  • What do you mean by ...goes back over the same sheets and adds new dataframes ? Does it mean append dataframes? Commented Apr 20, 2016 at 16:19
  • sorry, I meant it add already created dataframes to the excel sheet. Does not append anything. Commented Apr 20, 2016 at 16:24

4 Answers 4

7

frames and highcs are the same length, you can use zip() to iterate them at once:

for (f,frame), (h, highc) in zip(frames.items(), highcs.items()): # do your work here 

zip()

This function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.The returned list is truncated in length to the length of the shortest argument sequence.

Sign up to request clarification or add additional context in comments.

1 Comment

if the length of frames and highcs are not equal, can we still use zip()?
2

Use izip from itertools

from itertools import izip for (x, frame), (x, highc) in izip(frames.items(), highcs.items()): ... 

https://docs.python.org/2/library/itertools.html#itertools.izip

Make an iterator that aggregates elements from each of the iterables. Like zip() except that it returns an iterator instead of a list. Used forlock-step iteration over several iterables at a time.

izip is more efficient than zip because izip returns an iterator instead of a list. To make it even more efficient, replace .items() with .iteritems()

Comments

0

Not sure what you are doing, but maybe iterate over the groups again?

for group in groups: frame = frames[group] highc = highcs[group] ... 

Comments

0

Dunno how pythonic this approach is, but I can think of two quick and dirty ways to do it.

Are they guaranteed to be keyed with the same keys? If so, you could try:

for key in frames.keys(): doSomething(frames[key]) doSomethingElse(highcs[key]) 

Or, if not, do they have the same length? You could:

for i in range(len(frames.keys())): frameKey = frames.keys[i] highcsKey = highcs.keys[i] doSomething(frames[frameKey]) doSomethingElse(highcs[highcsKey]) 

Even if they aren't the same length, you could (and probably should) test the lengths of the keys() lists for both dictionaries anyway, and only index into the longer one once you pass the bound of the shorter one.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.