The Any Function or How to Analyze Tweets in Python

What is the output of this puzzle?


wordlist = ["bitcoin",
crawled_text = ”’
Research produced by the University of
Cambridge estimates that in 2017,
there are 2.9 to 5.8 million unique
users using a cryptocurrency wallet,
most of them using bitcoin.

s = crawled_text.split()
res1 = True in map(lambda x: x in s, wordlist)
res2 = any(x in wordlist for x in s)
print(res1 == res2)


Both res1 and res2 store whether the variable crawled_text contains a word from the wordlist.

res1: The map function checks for each element x in the wordlist whether x is an element of the split crawled_text. The default split function divides the string along the whitespaces. The result is an iterable with three booleans, one for each word in the wordlist.
Finally, we check whether one of them is True.

res2: The any function ckecks whether there is an element in the iterable that is True. As soon as it finds such a True value, this function returns True. Note that it is more efficient to use the any function to do this instead of performing a list iteration. After checking for the first word ‘bitcoin’, the function already returns True.

Hence, both variables store the same boolean value.

Are you a master coder?
Test your skills now!

Related Video




Leave a Comment

Your email address will not be published. Required fields are marked *