Fix the issue "RuntimeError: dictionary changed size during iteration"