0

I've been trying to make a nested for loop, but for some reason the inside one only loops once and I can't figure out why. I've already reduced my code to the bare minimum just to figure out whats happening:

pupil = db.session.query(Pupil).all()
result = db.session.query(Pupil_OLD).all()
for row in pupil:
    for sublist in result:   
        print("sublist"+str(sublist.PUPIL_ID))
    print("pupil"+str(row.PUPIL_ID))

This produces:

pupil1
sublist1
sublist2
sublist3
pupil2
pupil3

While it should produce

pupil1
sublist1
sublist2
sublist3
pupil2
sublist1
sublist2
sublist3
pupil3
sublist1
sublist2
sublist3

Does anyone have an idea what I'm doing wrong?

4
  • What does db.session.query(Pupil_OLD).all() return? Commented Jul 11, 2017 at 18:34
  • 1
    My guess is that by doing for sublist in result: you are consuming the query. Commented Jul 11, 2017 at 18:35
  • Is this SQLAlchemy? Commented Jul 11, 2017 at 18:35
  • in second line check the result for use simple print to see what is the contents of it. your problem relate to result. Commented Jul 11, 2017 at 19:06

2 Answers 2

3

I assume result is a generator which is reaching the end.

You can reset it like so:

pupil = db.session.query(Pupil).all()
for row in pupil:
    result = db.session.query(Pupil_OLD).all()
    for sublist in result:   
        print("sublist"+str(sublist.PUPIL_ID))
    print("pupil"+str(row.PUPIL_ID))

Converting it to a list is another option, but you'd be missing out on cool generators ;)

My love for generator's aside, if resetting it means re-querying a database, you might be better off with the list.

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

5 Comments

It is not very efficient to do the same query over and over for each pupil.
Probably depends if your constraints are speed or memory, but I agree that repeating the query is inefficient.
If memory is an issue then the results are very large and dumping it to disk memory would solve both problems.
Yeah, and you'd probably want to use a generator to dump it to disk, and a generator each time you want to read it.
I had never heard of a generator until now and the fact that you can exhaust it. I thought I was dealing with lists. I've converted the result generator to a list and now it works. Querying the database every time would be inefficient indeed. Time to do some reading on generators :) Thanks everyone!
1

You should rather do something like

pupil = db.session.query(Pupil).all()
result = list(db.session.query(Pupil_OLD).all())

So the result of db.session.query(Pupil_OLD).all() which appears to be a generator, is converted to a list which won't be consumed like your generator is.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.