How to add a triangulate modifier and apply it? Im trying to check if the selected object has a specific modifier, adding the modifier if it doesn't exist, and then applying all the modifiers:

import bpy

obj = bpy.context.object

modifs=[]
for modifier in obj.modifiers:
    modifs.append(modifier.type)

if 'TRIANGULATE' not in modifs:]
    bpy.ops.object.modifier_add(type='TRIANGULATE')

for modif in modifs:
    bpy.ops.object.modifier_apply(apply_as='DATA', modifier=modif)


Code above adds the modifier if it isn't present but it doesn't apply the modifiers to the object. If I paste bpy.ops.object.modifier_apply(apply_as='DATA', modifier=modif) into the python console it applies the modifier. What am I doing wrong?
The list doesn’t contain the newly added triangulate modifier (if there is one). Also, it seems necessary to apply by name, not by type. The following works for me:

import bpy

obj = bpy.context.object

modif_types = [ modifier.type for modifier in obj.modifiers ]

if 'TRIANGULATE' not in modif_types:
    bpy.ops.object.modifier_add(type='TRIANGULATE')

modif_names = [ modifier.name for modifier in obj.modifiers ]

for modif in modif_names:
    bpy.ops.object.modifier_apply(apply_as='DATA', modifier=modif)