code: def id_mapper(self, prop, filters=None, return_as_set=False):
          
          if filters:
              filter_str = "\n".join("?wditem wwdt:{} wwd:{} .".format(x[0], x[1]) for x in filters)
          else:
              filter_str = ""
          query = """
          PREFIX wwdt: <http://www.wikidata.org/prop/direct/>
          PREFIX wwd: <http://www.wikidata.org/entity/>
  
          SELECT ?localitem ?ext_id WHERE {{
            SERVICE <https://query.wikidata.org/sparql> {{
              ?wditem wwdt:{prop} ?ext_id .
              {filter_str}
            }}
            ?localitem wdt:P3 ?wditem
          }}
          """.format(prop=prop, filter_str=filter_str)
  
          results = wdi_core.WDItemEngine.execute_sparql_query(query, endpoint=self.sparql_endpoint_url)['results']['bindings']
          results = [{k: v['value'] for k, v in x.items()} for x in results]
          for r in results:
              r['localitem'] = r['localitem'].split('/')[-1]
          if not results:
              return None
  
          id_qid = defaultdict(set)
          for r in results:
              id_qid[r['ext_id']].add(r['localitem'])
  
          if return_as_set:
              return dict(id_qid)
          else:
              return {x['ext_id']: x['localitem'] for x in results}
docs: # see wdi_helpers.id_mapper for help on usage
          # QIDs returned are from the wikibase specified in self.sparql_endpoint_url
          # see WikibaseHelper.prop2qid for more details
          Example: Get all human genes:
          prop = "P351"
          filters = [("P703", "Q15978631")]
          h.id_mapper(prop, filters)