-1

I tried the following program in ipython

In [1]: a = (1,2,3,4,5,6,7)

In [2]: b = [1,2,3,4,5,6,7]

In [3]: a
Out[3]: (1, 2, 3, 4, 5, 6, 7)

In [4]: b
Out[4]: [1, 2, 3, 4, 5, 6, 7]

In [5]: a == b
Out[5]: False

In the above program what is the difference between a and b? Why is a==b returning False?

liv2hak
  • 14,472
  • 53
  • 157
  • 270
  • 3
    a is a tuple and b is a list. check this http://stackoverflow.com/questions/626759/whats-the-difference-between-list-and-tuples – user2963623 Aug 20 '14 at 07:36

4 Answers4

2

Your a is a tuple, uses round brackets (), while your b is a list, uses square brackets [], so they are not of the same data type and the comparison fails, although they contain the same items:

>>> a = (1,2,3,4,5,6,7) # round brackets declare a tuple
>>> b = [1,2,3,4,5,6,7] # square brackets declare a list
>>> type(a)
<type 'tuple'>
>>> type(b)
<type 'list'>
>>> a == b
False

For the comparison you need to get them to be of the same type first, then you can compare the contents:

>>> list(a) == b
True
>>> a == tuple(b)
True
>>> c = [1,2,3]
>>> b == c
False
famousgarkin
  • 13,687
  • 5
  • 58
  • 74
2

a and b have different types. Instead, try these:

a == tuple(b)
list(a) == b
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1

You are not comparing two lists, but a list with a tuple.

In [1]: a = (1,2,3,4,5,6,7)

In [2]: b = [1,2,3,4,5,6,7]

In [3]: type(a)
Out[3]: tuple

In [4]: type(b)
Out[4]: list
Bartosz Marcinkowski
  • 6,651
  • 4
  • 39
  • 69
0

In your program a is not list but tuple. That is why it is failing on the check. To check type you can use type function. You can use list function to typecast tuple to list for check as follows:

>>> a = (1,2,3,4,5,6,7)
>>> b = [1,2,3,4,5,6,7]
>>> 
>>> a == b
False
>>> type(a)
<type 'tuple'>
>>> type(b)
<type 'list'>
>>> 
>>> list(a) == b
True
Ansuman Bebarta
  • 7,011
  • 6
  • 27
  • 44