Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Iterate through a Python Flask-sqlalchemy Query Result as a list comprehension

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading