import json entity2id = "entity2id.json" entity2id = json.load(open(entity2id, "r")) itemids = "item_ids.json" itemids = json.load(open(itemids, "r")) itemnames = [k for k, v in entity2id.items() if v in itemids] with open("itemnames.json", "w") as f: json.dump(itemnames, f)