Instapy: like_by_tags using random.choice?

Created on 15 Dec 2017  路  4Comments  路  Source: timgrossmann/InstaPy

I've got a list of tags I'm targeting which are stored in the varible "like_tags":

like_tags = ['tag1', 'tag2', 'tag3]
I want to target each tag at random rather than simply going down throught he list in order each time I run the script. Is that possible?

I'm trying:

session.like_by_tags(random.choice(like_tags), amount=100)

The script runs, but it seems to target a completely different tag than any I've listed in my "like_tags" list. For example earlier, I ran the script and instead of targeting "tag1" first, it simply started with "#W" tag which isn't anywhere in my list of tags to target.

help wanted

Most helpful comment

Ah thank you very much. Makes sense!

All 4 comments

hey @deronsizemore ,

you can do it:

import random
like_tags = ['tag1', 'tag2', 'tag3]
like_tags = random.sample(like_tags, len(like_tags))

session.like_by_tags(like_tags, amount=100)

Thanks, that makes sense now that I see it and seems to work great. Can I ask why you chose to use random.sample vs random.choice? seems reading online that everyone recommends random.choice

it's because sample return a new reordered list, and choice will return only one element from like_tags

>>> x = [ 'aaa', 'bbb' , 'ccc']
>>> random.sample(x, len(x))
['aaa', 'ccc', 'bbb']
>>> random.sample(x, len(x))
['ccc', 'aaa', 'bbb']
>>> random.choice(x)
'ccc'
>>> random.choice(x)
'bbb'

Ah thank you very much. Makes sense!

Was this page helpful?
0 / 5 - 0 ratings