I have a flask-sqlalchemy query which returns a list of alert data.
alert_data = Alerts.query.filter_by(alertRemediated = False).all()
I am iterating through the result with the below code to count all "Critical" alerts.
This code works, but seem inefficient.
c = 0
for alert in alert_data:
if alert.alertSeverity == 'Critical':
c +=1
total_crit_alerts = c
I’ve tried the following list comprehension to make the code more streamlined, but I cannot get it to work.
total_crit_alerts = sum(1 for i in alert_data.alertSevertiy if i == 'Critical')
The error returned is: AttributeError: 'list' object has no attribute 'alertSevertiy'
Any suggestions would be greatly appreciated.
>Solution :
You need to iterate through alert_data list.
Also you have type in alertSeverity
You can try:
total_crit_alerts = sum(1 for i in alert_data if i.alertSeverity == 'Critical')