Python Forum

Full Version: Creating a loop with dynamic variables instead of hardcoded values
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The first step is creating a dataframe of usernames and their corresponding User IDs. You also need to login to an Instagram account for authentication purposes.

  from instagram_private_api import Client, ClientCompatPatch
    from operator import itemgetter
    import pandas as pd
    import json
    import requests

    user_name = ''
    password = ''
    api = Client(user_name, password)

    influencers = [['dcfoodcravings', '5577994064'], ['dcfoodlovers', '1902259438'], ['excellence.beauty.lounge', '2213626850'], ['foodzie', '1486757'], ['fiolamaredc', '1109579078']]

    influencers_df = pd.DataFrame(influencers, columns=['username', 'userId'])
Once that's complete, we then move on to getting a list of their followers. This is where the issue is. You can see for the variables following_username, following_userid, and get_followers I am using **hard coded values.** But what I'm trying to do is run all the userId's (along with their corresponding username's in the influencers_df dataframe in a loop with dynamic variables.

So the end product would be list of all the followers from those username/userid combinations in the influencers_df dataframe.

 def followers_users(userid_instagram):
        followers = []
        combined = []
        results = api.user_followers(userid_instagram, rank_token=api.generate_uuid())
        followers.extend(results.get('users', []))
        next_max_id = results.get('next_max_id')
        while next_max_id:
            results = api.user_followers(userid_instagram, rank_token=api.generate_uuid(), max_id=next_max_id)
            followers.extend(results.get('users', []))
            next_max_id = results.get('next_max_id')
        userid = [followers[i]['pk'] for i in range(0,len(followers))]
        full_names = [followers[i]['full_name'] for i in range(0,len(followers))]
        usernames = [followers[i]['username'] for i in range(0,len(followers))]
        profile_pic_url = [followers[i]['profile_pic_url'] for i in range(0,len(followers))]
        followers_text = ['follower' for i in range(0,len(followers))]
        following_username = ['dcfoodcravings' for i in range(0,len(followers))]
        following_userid = ['5577994064' for i in range(0,len(followers))]
        combined.extend([list(i) for i in zip(userid, full_names,
                                                usernames, profile_pic_url, followers_text,
                                                following_username, following_userid)])
        combined = sorted(combined, key=itemgetter(2), reverse=False)
        return(combined)
    get_followers = followers_users('5577994064')
    followers = pd.DataFrame(get_followers, columns = ['userID' , 'Full Name', 'username', 'Profile Picture', 'Type', 'following_username', 'following_userid'])
    followers
If you don't know how to iterate over values in a dataframe series you should write a small program that focuses on doing that. Toe in, not jump in. Jump in is ok if you have a strong programming background with another language and all you are learning is syntax and libraries, but from your code example above I don't think you have that.
(Jul-27-2022, 06:45 PM)deanhystad Wrote: [ -> ]If you don't know how to iterate over values in a dataframe series you should write a small program that focuses on doing that. Toe in, not jump in. Jump in is ok if you have a strong programming background with another language and all you are learning is syntax and libraries, but from your code example above I don't think you have that.

Okay, so how would you write the code I posted? I learn best by seeing what a final product would look like and then working backwards.
It is a style of Python coding that I've not come across before and must be a real mear to debug.

As an example, your code line...

followers_text = ['follower' for i in range(0,len(followers))]
... I would code as...

for i in range(len(followers)):
    followers_text.append('follower')
... that said, I don't see that it's doing anything other than add the string 'follower' to the list object followers_text[] for as many times as there are item in the list object followers[].

Where did you learn this style?