Search code examples
pythonloopsif-statementsap-ase

Python loop and if statment weird issues


Ok, I dont understand what is happening. I am looping over output from my query. A pretty normal loop and if. The issue is when I try to do results[o] < 30. It does not print out the variable. There are column data less than 30 for some reason they don't print. Even if I print a string. Nothing happens. The else statement will print but not the if. On my flask template I have similar code and it works. What am I doing incorrectly. The results[0] is of type int. So I don't know what the issue is. I am not getting any errors.

    for recordset in return_stored_procedure:
        results.append(dict(zip(sp_column_names, recordset)))
        if results[0]['days_column'] <= 30:
           print variable
        else:
           print variable

***update*****
Figured out the issues thanks everyone. Made the corrections now I will remove what I don't want from the list.

    for l in sp_results:
        if l['column'] < 30:
            newlist.append(l)

Now on to making a list comprehension. Instead of this loop.


Solution

  • It is hard to tell exactly what you want, but I assume you want to check the data_column key of the dict that you have just appended to results. Again, I'm not sure if this is what is desired, but you can get the last element from results by taking an index of -1:

    for recordset in return_stored_procedure:
        results.append(dict(zip(sp_column_names, recordset)))
        if results[-1]['days_column'] <= 30:
           print variable
        else:
           print variable