-1

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.

user3525290
  • 1,557
  • 2
  • 20
  • 47
  • 2
    well, you are always checking the first item in `results`, which is not changing in the loop you are showing, so there's no reason to expect that check to ever change.... – juanpa.arrivillaga Nov 27 '17 at 19:30
  • Also, if as you say `results[0]` is of type `int`, then `results[0]['days_column']` would throw a `TypeError`, because `int` objects are not sub-scriptable. So your question doesn't make a whole-lot of sense. I suggest you provide a [mcve] – juanpa.arrivillaga Nov 27 '17 at 19:34
  • @juanpa.arrivillaga You can see from the line before, the OP is appending a `dictionary` to the `results` `list` (I think): `results.append(dict(...))`. – Joe Iddon Nov 27 '17 at 19:41
  • @JoeIddon right, I see that, but that will not change what is in index 0 (except for maybe the first iteration). It almost certainly isn't what the OP wants, and again, it is not consistent with the OPs claim that `results[0]` is an `int` – juanpa.arrivillaga Nov 27 '17 at 19:44
  • @juanpa.arrivillaga Ah, yes I hadn't considered the first `index`. – Joe Iddon Nov 27 '17 at 19:45
  • @juanpa.arrivillaga I had a typo when I posted. Should have been result[c]. – user3525290 Nov 28 '17 at 13:45

1 Answers1

1

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
Joe Iddon
  • 20,101
  • 7
  • 33
  • 54