'issue with jinja array length

so here is the situation. I have the following code thats looping through some source data and appending unique values to an array

'{%- set sources = [] -%}
{%- for alert in cyber_alerts -%}
  {%- for se in alert['security_events'] -%}
    {%-  if (se['additional_properties']['source']) not in sources -%}
      {%- do sources.append(se['additional_properties']['source']) -%}
    {%- endif -%}
  {%- endfor -%}
{%- endfor -%}'

and then in my HTML table code I have something like this

'{%- if sources|length > 0  -%}
<tr>
  <td style="background-color:#ff0000;"><font color="white">Threat Source</td>
  <td>{{ sources|join(", ") }}</td>
</tr>
{%- endif -%}
'

However the problem I am facing is even if there are no source fields in the raw data the length of the array always comes back as 1 . I assumed it would be 0 if nothing was actually appended to it.

Essentially i only want the table row for Sources to appear if something was appended to the array. if i run the code against data set that has no Sources field it still creates the table row with the values of Sources being blank

please help. I am a newb with Jinja



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source