I’m working on a fairly simple script that monitors and archive’s a given users tweets — right now, my configuration requests an account’s 20 most recent tweets (API limit) every five minutes, then checks to see which of those are new, and appends the new ones to a file. should be simple.
all I’m doing right now is the following loop, with u being defined as the json output from the twitter api:
json_u = json.loads(u)
after first run:
q = [x for x in json_u if x not in old_u]
write q
old_u = json_u
wait five minutes
problem is that the q = line doesn’t seem to be doing the job right, as the q variable ends up containing all 20 new tweets every time. thinking that python was having a hard time with this construction (json_u should be a list of 20 dicts), I wanted to implement it this way instead:
q = [x for str(x) in json_u if str(x) not in old_u]
but it throws a
SyntaxError: can’t assign to function call error.
any help? thanks!
The syntax is [ expression for variables in … ], this is why you get the syntax error.
Your original list comprehension was fine. The problem seems to be that the dicts you have in jason_u are not identical to those in old_u.
Have you printed the dicts and looked for differences such as timestamps?