Python Forum
"any" retunes an error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: "any" retunes an error (/thread-31009.html)



"any" retunes an error - tester_V - Nov-17-2020

Hi, I'm getting an error:
'bool' object is not iterabler" when running this snippet and I do not understand why.
import os
tof = ['try','two']
with open('C:/02/file1.txt', 'r') as th_th :
    for x in tof :
        for ln in th_th :
            if any(x in ln) :
                print (ln)
And no errors when running this one (see below):

import os
tof = ['try','two']
with open('C:/02/file1.txt', 'r') as th_th :
    for ln in th_th :
        if any(x in ln for x in tof):     
            print (ln)
They look the same to me but apparently, they are not Sad.

Could anyone elaborate?

Thank you!


RE: "any" retunes an error - bowlofred - Nov-17-2020

any() takes something that can be iterated over, that you can make multiple tests on.

x in y is a single expression. It returns one value and isn't appropriate for any()

>>> "x" in "this string"
False
>>> any("x" in "this string")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'bool' object is not iterable
In addition, x and ln are static at this point in the code, so any() doesn't seem useful here. You're changing the variables in a loop and checking one thing each time through the loop.

For your second snippet, x in ln for x in tof, this is a generator comprehension and you can iterate over it. Therefore any() will return true if any of the elements are true.

>>> list(x in "The quick" for x in "abcdef")
[False, False, True, False, True, False]
>>> any(x in "The quick" for x in "abcdef")
True



RE: "any" retunes an error - tester_V - Nov-17-2020

To bowlofred,
Thank you for your reply!
it is still confusing.


RE: "any" retunes an error - perfringo - Nov-17-2020

(Nov-17-2020, 08:53 PM)tester_V Wrote: They look the same to me but apparently, they are not Sad.

it is still confusing.

These snippets of code are anything but same.

First one reads:

for every item in list:
    for every line in file


This means that for every item in list you go through all lines of file. If file is long and list is short this is not very optimal algorithm.

Second one reads:

for every line in file:
    is there any item from list in line
This means that you go through file line by line and for every line check whether any item list is present. any() has short circuit feature i.e. it stops after first True.

I don't know whether it is a problem or not, but if line is 'computer network' then current code finds 'two' from that line.


RE: "any" retunes an error - bowlofred - Nov-17-2020

python comprehensions can be confusing at first if you're not used to them. They're a very compact way to express a collection of info.

You don't have to use them. You could just write it as a regular loop.

Explicit loop:
tof = ['try','two']
with open('C:/02/file1.txt', 'r') as th_th :
    for line in th_th:
        for word in tof:
            if word in line:
                print(line)
                break
Comprehension:
tof = ['try','two']
with open('C:/02/file1.txt', 'r') as th_th :
    print([line for line in th_th if any(word in line for word in tof)])



RE: "any" retunes an error - tester_V - Nov-18-2020

Guys thank you for the explanations!
I'm new to Python and I'm not a programmer and yes, it is confusing for now...

I really appreciate your help!