Using removeKerningForPair() crashes Glyphs

Hello,
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,
Tom

k = f.kerning
q = f.selectedFontMaster.id

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

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

f.disableUpdateInterface()
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
	else:
		for g in f.glyphs:
			if g.id == b:
				b = g.name
				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'
			else:
				for g in f.glyphs:
					if g.id == s:
						s = g.name
						if s in l:
							flagR = 'y'

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

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!