MKK
MKK

Reputation: 49

How to access a value inside an array of dictionaries?

I've downloaded 10 followers for each of the two accounts in the array and putted them inside a dictionary and then inside an array. Now i need to access 4 of the followers screen name of the first acc and 4 of the followers screen name of the second but when i try so it gives me an error. Below is the code i used and the result of the download of the followers

twitter_accounts = ["AccName1", "AccName2"]
res = {}
pbar = tqdm_notebook(total=len(twitter_accounts))

for twitter_account in twitter_accounts:
    inner_structure = []
    for page in tweepy.Cursor(api.followers, screen_name=twitter_account,
                              skip_status=True, include_user_entities=False).items(10):
        val = page._json
        inner_dict = {}
        inner_dict["name"] = val["name"] 
        inner_dict["screen_name"] = val["screen_name"]
        if inner_dict not in inner_structure:
            inner_structure.append(inner_dict)

    res[twitter_account] = inner_structure
    pbar.update(1)

pbar.close()
print("done.")
for twitter_account in twitter_accounts:

    #print(len(res[twitter_account]))
    display(res[twitter_account]['screen_name'])

Below im going to upload the photo of the result of display(res[twitter_account])enter image description here

Also im uploading the error i get when i try to access display(res[twitter_account]['screen_name'])enter image description here

I want to access the screen name of the followers i got so i can download from 4 of them their followers. So the followers of Followers of the first 2 accounts

Upvotes: 0

Views: 455

Answers (2)

user212514
user212514

Reputation: 3130

It looks like you're trying to access the array

for twitter_account in twitter_accounts:
    inner_structure = res[twitter_account]  # access the array
    for inner_dict in inner_structure[:4]:  # go through the first 4 elements
        display(inner_dict['screen_name'])

Upvotes: 1

Alexander Pushkarev
Alexander Pushkarev

Reputation: 1145

Provided that you have >= 4 followers:

for twitter_account in twitter_accounts:
    for in in range(4):
        display(res[twitter_account][i]['screen_name'])

Upvotes: 1

Related Questions