Question: how can I get the last item in a python generator in a fast and memory-efficient way?
MWE:
import snscrape.modules.twitter as sntwitter
import time; start = time.time()
query = "Rooty Roo"
obj = sntwitter.TwitterSearchScraper(query)
print(obj) # didn't see much useful besides get_items
cnt = 0
items = obj.get_items()
for item in items:
cnt += 1
if cnt % 100 == 0:
print(cnt)
# end if
# end for
## the above seems ideal for memory-efficiency but
## maybe super slow as I have no idea if there are
## millions or billions of tweets in there.
## Been running a few minutes and at ~17k so far.
## Not super ideal for playing around...
print(vars(tweet))
print("tweets: ", cnt)
print("executed in: ", time.time() - start)
I guess the above is not a super MWE since it relies on a package, but this is the first time I've encountered a generator. And is what prompted this question :)
Context: I'm trying to learn more about how this package works. I started reading the source but thought playing around and inspecting the data might be faster ¯\(ツ)/¯
Memory-Efficient Context: my laptop is turning 10 this year and I think part of the RAM is failing. Theoretically it has 8 GB RAM but using more than 1-2 GB causes browser pages to crash :D
Is this question answered already? Probably, but google search results for 'python get last item of a generator' return results for iterators...