Python Forum
Problem with comparison operator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with comparison operator
#1
I'm writing a program that converts a blocklist into a BIND9 zones file for a blackhole/sinkhole DNS server. Within the script I have code similar to the following:

# The following list is just for the sake of illustration, the actual list comes from reading the blocklist file
my_list = ["foo.com/index.html", "bar.com/downloads/malware.exe", "conteso.com/phishing/email.htm", "domain.com/blog/test", "[Adblock]", "another.domain.com/another/test/blog", "blah.net/blah/blah/blah"]

for i in my_list:
    if(i != "[Adblock]"):
        print("This block should only execute if i is NOT equal to [Adblock].")
The issue is that the if block ALWAYS executes, whether i is equal to [Adblock] or not. I've tried escaping the brackets ("\[Adblock\]") but that didn't help either. I also tried single quotes and single quotes while escaping the brackets.

Any help would be greatly appreciated.
Reply
#2
It works OK, all I've added is an else
# The following list is just for the sake of illustration, the actual list comes from reading the blocklist file
my_list = ["foo.com/index.html", "bar.com/downloads/malware.exe", "conteso.com/phishing/email.htm", "domain.com/blog/test", "[Adblock]", "another.domain.com/another/test/blog", "blah.net/blah/blah/blah"]
 
for i in my_list:
    if(i != "[Adblock]"):
        print("This block should only execute if i is NOT equal to [Adblock].")
    else:
        print("This block should only execute if i is equal to [Adblock].")
Outputs
Output:
This block should only execute if i is NOT equal to [Adblock]. This block should only execute if i is NOT equal to [Adblock]. This block should only execute if i is NOT equal to [Adblock]. This block should only execute if i is NOT equal to [Adblock]. This block should only execute if i is equal to [Adblock]. This block should only execute if i is NOT equal to [Adblock]. This block should only execute if i is NOT equal to [Adblock].
Reply
#3
Thank you for looking into this Yoriz.
I haven't tried an else there. What I found that did work (and it contains an else Big Grin ) is:
# The following list is just for the sake of illustration, the actual list comes from reading the blocklist file
my_list = ["foo.com/index.html", "bar.com/downloads/malware.exe", "conteso.com/phishing/email.htm", "domain.com/blog/test", "[Adblock]", "another.domain.com/another/test/blog", "blah.net/blah/blah/blah"]
  
for i in my_list:
    if(re.match('\[Adblock\]', i)):
        next
    else:
        print("This should print only if i is NOT equal to [Adblock].")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python greater than equal to comparison operator Ilangos 4 2,426 Sep-26-2020, 03:53 AM
Last Post: buran
  Comparison Operator "is" idle vs python command spisatus 3 2,791 Oct-29-2019, 10:00 PM
Last Post: DeaD_EyE
  if statement and in operator problem bobger 5 4,035 Nov-30-2017, 06:50 PM
Last Post: bobger

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020