Search code examples
pythondjango-templatesjinja2

Problem with nested loops and if else statements in python, Jinja2


The following code is doing what it is supposed to do, but I need it to return just one instance under each category; please see the output example below.

categories = [[1],
              [2],
              [3],
              [4],
              [5],
              [6],
              [7],
              [8]]

files = [(5, 1, 25, 1, 'file.pdf'),
         (6, 1, 25, 3, 'file.pdf'),
         (7, 1, 25, 6, 'file.pdf'),]


for c in categories:
    print(f'Category: {c[0]}')
    for f in files:
        if c[0] == f[3]:
            print(f)
        else:
            print('Standard text block B')

The code returns the following.

Category: 1
(5, 1, 25, 1, 'file.pdf')
Standard text block B
Standard text block B
Category: 2
Standard text block B
Standard text block B
Standard text block B
Category: 3
Standard text block B
(6, 1, 25, 3, 'file.pdf')
Standard text block B
Category: 4
Standard text block B
Standard text block B
Standard text block B
Category: 5
Standard text block B
Standard text block B
Standard text block B
Category: 6
Standard text block B
Standard text block B
(7, 1, 25, 6, 'file.pdf')
Category: 7
Standard text block B
Standard text block B
Standard text block B
Category: 8
Standard text block B
Standard text block B
Standard text block B

But I am looking for a solution to return the following:

Category: 1
(5, 1, 25, 1, 'file.pdf')
Category: 2
Standard text block B
Category: 3
(6, 1, 25, 3, 'file.pdf')
Category: 4
Standard text block B
Category: 5
Standard text block B
Category: 6
(7, 1, 25, 6, 'file.pdf')
Category: 7
Standard text block B
Category: 8
Standard text block B

I want to accomplish it with loops with Jinja2, but I think the solution needs to be with Python first.


Solution

  • I tried out your code and it replicated the continual additional printing. Analyzing the code, it would appear that some additional conditional testing would need to be done to skip the extraneous lines being printed. With that in mind, following is a simplistic refactoring of your program to provide the wanted output.

    categories = [[1], [2], [3], [4], [5], [6], [7], [8]]
    
    files = [(5, 1, 25, 1, 'file.pdf'),
             (6, 1, 25, 3, 'file.pdf'),
             (7, 1, 25, 6, 'file.pdf'),]
    
    for c in categories:
        print(f'Category: {c[0]}')
        found = False                   # Introduce a boolean value to condition printing default
        for f in files:
            if c[0] == f[3]:
                print(f)
                found = True
        if not found:
            print('Standard text block B')
    

    Seeing no constrictions on what methods could or could not be used, a simple Boolean variable was added to determine if a category match was found. And if so, skip printing out the "Standard text block B" verbiage. Following was the test output at my terminal.

    craig@Vera:~/Python_Programs/Catagories$ python3 Catagory.py clear
    Category: 1
    (5, 1, 25, 1, 'file.pdf')
    Category: 2
    Standard text block B
    Category: 3
    (6, 1, 25, 3, 'file.pdf')
    Category: 4
    Standard text block B
    Category: 5
    Standard text block B
    Category: 6
    (7, 1, 25, 6, 'file.pdf')
    Category: 7
    Standard text block B
    Category: 8
    Standard text block B
    

    Give that an analysis to see if that meets your criteria. And view any comments that might be added. This is just one way to get to your end goals, but others might produce something slicker.