from collections import defaultdict
all_warnings = defaultdict(list)
for w in doc.GetWarnings():
description = w.GetDescriptionText()
all_warnings[description].append(w)
for warning_type in all_warnings:
warnings = all_warnings[war_typ]
for w in warnings:
element_ids = list(w.GetFailingElements()) + list(w.GetAdditionalElements())
elements = [doc.GetElement(e_id) for e_id in element_ids]