-2

A query produces multiple lines answer, not always all lines have any output/result, and would produce an empty row []

I want to

  1. when any line has a result to be shown and ignore all other empty rows [Case 1]
  2. if all rows are empty print out one single message. [Case 2]

I have tried a number of options with loop statement if & else and all() or any() however my best shot is to fulfil the first requirement only- I am unable to get the second requirement for all empty.

a snippet of the code that only delivers the 1st requirement

    f = query(data)
    for x in f:
        if x:
            print(x)
        else:
            if all(x):
               print("no f found")

also, I have tried both any() and all() and their not variation with no luck

Case 1 the output of f query, when there are some results, [in this case only 2 results out of 4]

['p=SDVjXSERgCziEsyhT4dIySuh9ha5Udt5b9F5TATeJ']
[]
['p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC31']
[]

desire output

p=SDVjXSERgCziEsyhT4dIySuh9ha5Udt5b9F5TATeJ
p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC31

Case 2, the output of f query, when there is no result at all

[]
[]
[]
[]

desire output

no f found

**What I don't want is to get a ** no f found for each row that is empty

p=SDVjXSERgCziEsyhT4dIySuh9ha5Udt5b9F5TATeJ
no f found
p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC31
no f found

or

no f found
no f found
no f found
no f found

would really appreciate if anyone can give me a hand

M H
  • 1
  • 3

2 Answers2

0

You can solve this easily by introducing a variable, found:

found = False
f = query(data)
for x in f:
    if x:
        print(x)
        found = True

if not found:
    print("no f found")

'if not any(f)' might work too, but it's fine to use a variable like that when you first traverse the list anyway, so you get the info without doing it again with any().

With any() this might be a nice way:

f = query(data)
if not any(f):
    print("no f found")
else:
    for x in f:
        if x:
            print(x)
antont
  • 2,676
  • 1
  • 19
  • 21
  • i tried both options and still give `no f found` for all lines ```no f found no f found no f found no f found ``` – M H Aug 27 '20 at 23:20
  • that's not possible - in my examples the 'no f found' print is outside the 'for' loop, it can only be outputted once. – antont Aug 27 '20 at 23:21
  • 1
    i think i got it now - i had another def and when i moved the not found outside the main def, it worked, thanks a lot – M H Aug 27 '20 at 23:31
0

If I understand your question correctly, you can simply use a flag to indicate if there are any results. If the flag is raised, only the results will be printed. Otherwise, "no f found" will be printed:

array = [['p=SDVjXSERgCziEsyhT4dIySuh9ha5Udt5b9F5TATeJ'],
     [],
     ['p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC31'],
     []]

flag_some = False
for a in array:
      if a:
            print(a)
            flag_some = True

if not flag_some:
      print("no f found")
mgmussi
  • 520
  • 1
  • 4
  • 19
  • problem is still it gives output for each line, so 4 line of no f found rather than one single line – M H Aug 27 '20 at 23:27
  • thanks, i had to move the flag outside the current def, and it worked - cheers – M H Aug 27 '20 at 23:32