You can use a while loop for that :
#since_id - Returns results with an ID greater than #(that is, more recent than) the specified ID. There are limits to the #number of Tweets which can be accessed through the API. # If the limit of Tweets has occured since the since_id, # the since_id will be forced to the oldest ID available. # max_id - Returns results with an ID less than (that is, older than) #or equal to the specified ID. Dict2 = dict({'@TweeetLorraine':1392217841680764931}) d2 = {} rep=[] from tqdm import tqdm for key, value in tqdm(Dict2.items()): for i in tweepy.Cursor(api.search, q='to:{} -filter:retweets"'.format(key),lang="en" ,since_id=value,tweet_mode='extended', wait_on_rate_limit=True, wait_on_rate_limit_notify=True).items(50): if (i.in_reply_to_status_id == value): rep.append(i) from pandas.io.json import json_normalize dfflat = pd.DataFrame() for tweet in rep: df_for_tweet = json_normalize(tweet._json) dfflat=dfflat.append(df_for_tweet,ignore_index=True,sort=True) d2.update(zip(dfflat["user.screen_name"].tolist(), dfflat["id"].tolist())) d2
For your use case, here is roughly the code that does what you describe, there is better ways to do that using map, I let you search for it if you want to know more.
Also, I'm not sure whether you want to completely clear the dict or only clear the current "i", but I think you can modify the following snippet to your true needs
mydict = initial_dict # while there is something in the dictionary while mydict: value_searched = None for key, value in mydict.items(): for i in tweepy.Cursor(api.search, q='to:{} -filter:retweets"'.format(key),lang="en" ,since_id=value,tweet_mode='extended', wait_on_rate_limit=True, wait_on_rate_limit_notify=True).items(50): if (i.in_reply_to_status_id == value): replies3.append(i) value_searched = i break break # create new dict from value retrieved mydict = {"@" +value_searched.user.screen_name : value_searched.id_str}
Edit2 : Using recursivity
def tweepy_stub(key, value): if key == "TweeetLorraine" and value == 1392217841680764931: return [ ("AlexBC997", 1392385334155956226), ("ChapinDolores", 1392432099945238529), ] elif key == "AlexBC997" and value == 1392385334155956226: return [("test", 139238533415595852)] elif ("ChapinDolores", 1392432099945238529): return [] def recursive(list_values, nb_recursion): mydict = {} if list_values == None or nb_recursion == 0: return mydict else: for name_user, tweet_id in list_values: mydict[(name_user, tweet_id)] = recursive( retrieve_direct_reply_stub(name_user, tweet_id), nb_recursion - 1 ) return mydict class stub_tweepy_answer: def __init__(self, status_id) -> None: self.in_reply_to_status_id = status_id def retrieve_direct_reply_stub(name_user, tweepy_id): rep = [] d2 = [] return tweepy_stub(name_user, tweepy_id) def retrieve_direct_reply(name_user, tweet_id): rep = [] d2 = [] for i in tweepy_stub(name_user, tweet_id): val = i if (i.in_reply_to_status_id == tweet_id): rep.append(i) from pandas.io.json import json_normalize dfflat = pd.DataFrame() for tweet in rep: df_for_tweet = json_normalize(tweet._json) dfflat=dfflat.append(df_for_tweet,ignore_index=True,sort=True) d2.append(zip(dfflat["user.screen_name"].tolist(), dfflat["id"].tolist())) return d2 #print(retrieve_direct_reply_stub("TweeetLorraine", 1392217841680764931)) elem = [("TweeetLorraine", 1392217841680764931)] print(recursive(elem, 3))