import pywikibot
import time
from pywikibot import pagegenerators

wikidata_site = pywikibot.Site('wikidata', 'wikidata')
repo = wikidata_site.data_repository()

inputdata = []
dataset_query = """SELECT DISTINCT ?item WHERE { ?item p:P1087/wikibase:rank wikibase:PreferredRank }"""
for itemkey in pagegenerators.WikidataSPARQLPageGenerator(dataset_query, site=wikidata_site):
    inputdata.append(itemkey.title())

for i, item in enumerate(inputdata):
    Qitem = pywikibot.ItemPage(repo, item)
    Qitem.get()
    
    print()
    print('=== Item {} ({}/{}) ==='.format(item, i+1, len(inputdata)))
    for claim in Qitem.claims['P1087']:
        claim.changeRank('normal', summary='remove preferred rank')
print()
print('All done, task finished')
=== Item Q27533831 (1/1) ===

All done, task finished