Using removeKerningForPair() crashes Glyphs

I’ve written a script to remove kerns for glyphs not in a list. This will crash Glyphs after a few iterations, but if I comment out removeKerningForPair(), it prints what I want to remove and finishes. Any ideas on what’s here that is incompatible?
Many thanks,

k = f.kerning
q =

l = ['A', 'B', '', '']

print('Removing kerns from list...')

for keyL in k[q].keys():
	flagL = 'n'
	# for classes
	b = keyL
	if '@' in b:
		for char in l:
			if char in f.glyphs:
				if f[char].rightKerningKey == b:
					flagL = 'y'
	# for glyph ids
		for g in f.glyphs:
			if == b:
				b =
				if b in l:
					flagL = 'y'

	if flagL == 'n':
		for keyR in k[q][keyL].keys():
			flagR = 'n'
			s = keyR
			if '@' in s:
				for char in l:
					if char in f.glyphs:
						if f[char].leftKerningKey == s:
							flagR = 'y'
				for g in f.glyphs:
					if == s:
						s =
						if s in l:
							flagR = 'y'

			if flagR == 'n':
				print('Removing', b, s)
					f.removeKerningForPair(q, b, s)
				except Exception as e:
					print("ERROR: %s %s (%s)" % (b, s, e))

Could you send me (privately) a font that triggers the problem?

Will do, Georg. Thanks

The problem is that you iterate the kerning and change it inside that loop. I would collect the arguments in a new list and call the remove function in a separate loop. Or make a copy of the kerning keys beforehand.

I think I understand. Many thanks, Georg!