0

I have two lists, data1 and data2:

data1 = [('hello world', [])]

data2 = [('hello world', 'greetings')]

I want to compare the values at the 2nd position of each list.

I am using the following piece of code to compare them, but it is printing only "a equals to c" but not "b equals to d" and it is printing the following result:

"Value_a: hello world, Value_b: [],Value_c: hello world,Value_d: greetings"

for a,b in data1:
    for c,d in data2:
        print("Value_a: %s, Value_b: %s,Value_c: %s,Value_d: %s,"%(a,b,c,d))
        if a is c:
            print("a is equal to c")
            if b is d:
                print("b is equal to d")
                count+= 1
                print("Count = ",count)
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
maria.m
  • 110
  • 7
  • 3
    Why *would* it print 'b equals to d'? In what way is an empty list equal to (or identical to, which is what you test with `is` - equality is `==`) the string `'greetings'`? – jonrsharpe Apr 25 '17 at 14:31
  • 2
    also, you have strings inside a tuple inside a list. The first element of `data1` is `('hello world', [])`, and there is no second element. – Nils Werner Apr 25 '17 at 14:32
  • 2
    Check your indentation. `b is d` will only trigger if `a is c` also because of the indentation – TLOwater Apr 25 '17 at 14:33
  • You're already comparing the values in the second position accurately, since `[] != 'greetings'`. Can you clarify what you need to know? – Erica Apr 25 '17 at 14:37
  • And `if b is d` the program will throw `NamError` because `count` is not declared. – cezar Apr 25 '17 at 14:37
  • It looks like you want to pair items at the same index from each list. If so, `for d1, d2 in zip(data1, data2):print("'{}' is {}equal to '{}'".format(d1, ('' if d1==d2 else 'not '), d2)` works provided you change `data1` and `data2` to be lists of string and not lists of tuples of string. – Steven Rumbalski Apr 25 '17 at 14:51
  • If you only want to show when they are equal do `for d1, d2 in zip(data1, data2):if d1 == d2:print("'{}' is equal to '{}'".format(d1, d2)` – Steven Rumbalski Apr 25 '17 at 14:53

3 Answers3

1

...it is printing only a equals to c but not b equals to d...

1 - b is d only executes if a is c is true, you need to fix the indentation

2 - is will return True if two variables point to the same object, == if the objects referred to by the variables are equal.


You'll probably end-up with something like:

for a, b in data1:
    for c, d in data2:
        print("Value_a: %s, Value_b: %s,Value_c: %s,Value_d: %s," % (a, b, c, d))
        if a == c:
            print("a is equal to c")
        if b == d:
            print("b is equal to d")
            count += 1
            print("Count = ", count)

Note:

Is there a difference between == and is in Python?

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

Yours should work for your example, but to compare longer lists pairwise

for (a,b),(c,d) in zip(data1,data2):

    print("Value_a: %s, Value_b: %s,Value_c: %s,Value_d: %s,"%(a,b,c,d))
    if a is c:
        print("a is equal to c")
        if b is d:
            print("b is equal to d")
            count+= 1
            print("Count = ",count)
Serge
  • 3,387
  • 3
  • 16
  • 34
0

Maybe you need to read this: Understanding Python's "is" operator

Besides, data2 contains two strings, meanwhile data1 contains one string and one empty lists.

Community
  • 1
  • 1
LlaveLuis
  • 88
  • 2
  • 5