0

I am trying to print the word found in the list from a website response, else if not found print "Not Found". However my script prints the word it found. But it also prints "Not Found" for every item in the list. I just need it to print "Not Found" if nothing from the list found.

My Script:

response = requests.post(URL, headers=Headers, cookies=Cookies, data=Data)

content = response.content
status_type = [b'Approved', b'Pending', b'Rejected', b'Issued']

for status in status_type:
    if status in content:
        print(status.decode())
    if status not in content:
        print("Not Found")

Output of My Script: enter image description here

  • An object will either be in the list or it will not be in the list. So your second statement should use `elif` instead of `if` – Jdeep Oct 06 '20 at 02:59
  • 1
    @Noah, actually an `else` with no condition would do that trick as well but the problem is actually that it prints not found for *each* status not in the content. I think what OP was after was to only print not found if *none* of them were found. – paxdiablo Oct 06 '20 at 03:04
  • @paxdiablo , Oh I see what he meant now. – Jdeep Oct 06 '20 at 03:12
  • Searching an item in a list is a very common algorithm, available in myriad places on line. Please research the topic before posting a question here. – Prune Oct 06 '20 at 03:18
  • Does this answer your question? [How can you search for an item in a list - python](https://stackoverflow.com/questions/60825971/how-can-you-search-for-an-item-in-a-list-python) – Russ Thomas Oct 06 '20 at 04:43

3 Answers3

1

The most obvious approach is simply to use a flag to see if any were found:

found = False
for status in status_type:
    if status in content:
        print(status.decode())
        found = True
        # break if you only want the first one found
if not found:
    print("Not Found")
paxdiablo
  • 814,905
  • 225
  • 1,535
  • 1,899
0

The flag approach of paxdiablo's answer is probably the most straightforward; another approach would be to put to gather the found statuses in a list, then handle it:

found_statuses = [status.decode() for status in status_type if status in content]

if found_statuses:
    print(', '.join(found_statuses))
else:
    print('Not Found')

This would be particularly useful if you needed to take special action if more than one status is found and need to not print them in that case (or print them in a different way):

found_statuses = [status.decode() for status in status_type if status in content]

if len(found_statuses) == 1:
    print(found_statuses[0])
elif len(found_statuses) > 1:
    print("Conflicting statuses: %s" % ' and '.join(found_statuses))
else:
    print('Not Found')
Jiří Baum
  • 5,928
  • 2
  • 15
  • 16
0

You can also store your results in another list and print

result = [status.decode() for status in status_type if status in content]
if len(result) == 0:
   print("Not Found")
else:
   for status in result:
       print(status)
Srishti Rawal
  • 594
  • 1
  • 4
  • 13