Reputation: 49
Im downloading the followers from 2 twitter accounts and putting them into a list of dictionaries. I downloaded 10 followers from account1 and 10 followers from account2. And with the following code i take the first 4 followers of account 1 and the first 4 of account2 and display them
twitter_accounts = ["account1", "account2"]
res = {}
follower = []
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()
for twitter_account in twitter_accounts:
for i in range(4):
display(res[twitter_account][i]['screen_name'])
So the final result will be the displaying of the first 4 followers of acc1 and the first 4 of acc2.
But what i really need to do is take those 8 strings and instead of displaying them storing them into an array.
I tried this way but i get an index out of range error.
for twitter_account in twitter_accounts:
for i in range(4):
for j in range(8):
follower[j]= res[twitter_account][i]['screen_name']
How can i store them in an array without getting the error?
Upvotes: 0
Views: 46
Reputation: 4649
You can either declare the list first, and append elements to it:
followers = []
for twitter_account in twitter_accounts:
for i in range(4):
followers.append(res[twitter_account][i]['screen_name'])
Or use a list comprehension directly (which I believe works but I can't test right now):
followers = [res[twitter_account][i]['screen_name'] for i in range(4) for twitter_account in twitter_accounts]
Whichever you find clearer and more readable (-:
Upvotes: 1