0

I have two lists

circles_plotted = [1]
circles_next_plotted = [1]

Numbers are appended to list circles_plotted each time a circle is plotted. However, is the correct notation for and item in each of the lists to be equivalent? (this will be in an if statement):

(item in circles_next_plotted)==(item in circles_plotted)
lyche
  • 125
  • 2
  • 10
  • 1
    Just do `circles_plotted == circles_next_plotted`, Python will internally takes care of the shallow comparisons. – thefourtheye Nov 23 '15 at 10:24
  • @thefourtheye Will this still work when multiple values are in `circles_plotted` so not all the values inside the list are equivalent? – lyche Nov 23 '15 at 10:26

2 Answers2

1

To check if lists are equal you can use ==. If position of elements differ you can check if sorted lists are equal:

li1 = [1, 2, 3]
li2 = [1, 2, 3]

print(li1 == li2)  # True

li3 = [3, 2, 1]
li4 = [1, 2, 3]

print(li3 == li4)  # False
print(sorted(li3) == sorted(li4))  # True

Also you can check whether the multisets with the elements of li3 and li4 are equal:

import collections

li3 = [3, 2, 1]
li4 = [1, 2, 3]

print(collections.Counter(li3) == collections.Counter(li4))  # True
pythad
  • 4,241
  • 2
  • 19
  • 41
-1

The notation is incorrect.

(item in circles_next_plotted)==(item in circles_plotted)

is a NameError in Python:name 'item' is not defined.

What you probably meant is

>>> (item for item in circles_next_plotted)==(item for item in circles_plotted)
False

This is because (item for item in circles_next_plotted) is a generator expression and the two generators are two distinct objects, thus not equal.

>>> (item for item in circles_next_plotted)
<generator object <genexpr> at 0x000000D50AF46BF8>
>>> (item for item in circles_plotted)    
<generator object <genexpr> at 0x000000D50AF46CA8>

To compare the lists you should use a simple comparison with the == operator which has been implemented for lists to do a per item comparison rather than a object identity check:

>>> circles_next_plotted==circles_plotted
True
Sebastian Wozny
  • 16,943
  • 7
  • 52
  • 69