user1470034
user1470034

Reputation: 691

Best way to handle a nested for loop?

I'm attempting to save records to my database based on the selected options in a form.

The code i'm trying to use keeps telling me that j does not exists in the and j in previouscourselist.

if request.method == 'POST':
    for i in courselist and j in previouscourselist:
        requestsave(courselist and = i, previouscourselist= j)

Using the following below. The records loops through correctly for the first part using for i in courselist . previouscourselist will be wrote as a list [####,####,####] etc. However, this is not what I want.

   if request.method == 'POST':
        for i in courselist
            requestsave(courselist = i, previouscourselist= previouscourselist)

What is the proper way to nest a forloop, so it loops through the courselist list and the previouscourselist? Writing individual records where there is a different courselist and previouscourselist?

What i'm trying to accomplish is to have my requestsave(courselist = i, and previouscourselist = j) So it's written like this:

CourseID   PreviousCourseID
2000       1001
2000       1015
2001       1001
2001       1015
2002       1001
2002       1015

Current using

  if request.method == 'POST':
        for i in courselist
            requestsave(courselist = i, previouscourselist= previouscourselist)

It writes the statement above to the database as

CourseID    PreviousCourse
2000        [1001, 1015]
2001        [1001, 1015]
2002        [1001, 1015]

Upvotes: 0

Views: 363

Answers (2)

Matt_G
Matt_G

Reputation: 516

First, your syntax suggests you want to store the index rather than the item itself. If you want enumerations, the syntax will be:

if request.method == "POST":
    for item_course, i in enumerate(courselist):
        for item_prev_course, j in enumerate(previouscourselist):
            requestsave(courselist=i, previouscourselist=j)

Otherwise, if you want to store the item, it's a good idea to use better variable names so that your code is more easily interpreted by readers.

if request.method == "POST":
    for current_course in courselist:
        for prev_course in previouscourselist:
           requestsave(courselist=current_course, previouscourselist=prev_course)

If you want to join the lists into a dictionary where there is a one to one relationship, you can use the zip() function like so:

if request.method == "POST":
    course_dict = dict(zip(courselist, previouscourselist)

and then iterate along that dictionary

   for course, prev_course in course_dict:
       requestsave(courselist=course, previouscourselist=prev_course)

Upvotes: 3

Alfe
Alfe

Reputation: 59426

Do you mean this:

if request.method == 'POST':
    for i in courselist:
        for j in previouscourselist:
            requestsave(courselist=i, previouscourselist=j)

If this solves your issue: There are other ways to achieve your goal, but if you did not know how to do this yet, then they are probably not a good alternative for you.

Upvotes: 1

Related Questions