Ok so I think your code DOES keep legendaries:
This basically means, I think, that if the item in your inventory is in the list, OR the quality is legendary, then keep = true (keep means do not identify)
However, some of your later code puts qualification on this, for example, all ilvl 60...