I’m working on a silly side project and have encountered an issue with lists and loops that I haven’t been able to wrap my head around.
I think it’s easier to explain with some pseudocode, so here’s what I’m starting with:
source_list = ['a', 'e', 'f', 'g'] list1 = ['a', 'b', 'c'] list2 =  counter = 0
What I’m trying to do:
- While counter is less than 3, pick a random item from
- Check if that item is in either
- If it IS in either list, go back to number 1 and start again
- If it is NOT in either list already, add it to
list2, counter +=1, and then go back to number 1
or to express that in pseudocode:
while counter < 3: source_list_choice = random.choice(source_list) check and be sure source_list_choice is not already in list1 or list2 if it IS in either one of those lists: start again by picking a new source_list_choice if it is NOT in either list: add it to list2 counter +=1 start again by picking a new source_list_choice
Why I want to do this: I’m building a Twitter bot that’ll tweet messages from a source list. I want to choose three items from that list at random, check that they haven’t already been tweeted before (list1), check the same tweet hasn’t been randomly picked twice by checking what’s already on the “today’s tweets” list (list2), and then come out of this bit of code with
list2, a list of three tweets that (1) are all unique and (2) haven’t been posted before.
What actual code do I have right now:
while counter < 3: print(counter) potential_tweet = random.choice(generated['Text']) for row in posted_tweets: if potential_tweet == row: break else: continue todays_tweets.append(potential_tweet) print("Added " + potential_tweet) counter += 1
What does this code do right now?
It seems to be counting by twos, and adding each randomly-selected tweet to
list2) twice. So I’m ending up with an output that’s like
['b, 'b', 'c', 'c'], but what I’m looking for is an output like
['b', 'c', 'd'], where b,c,d, are unique, randomly-selected items from
source_list) that aren’t in the
Any ideas? It feels like there’s some simple thing that I’m missing here. I also suspect there’s probably a more efficient way to do this than with for loops, which might become important as the list of already-posted tweets gets longer…