title: Python ordered list search versus searching sets for list of objects
body: <p>I have two lists of objects. Let's call the lists a and b. The objects (for our intents and purposes) are defined as below:</p>
  
  <pre><code>class MyObj:
      def __init__(self, string: str, integer: int):
          self.string = string
          self.integer = integer
  
      def __eq__(self, other):
          if self.integer == other.integer:
              pass
          else:
              return False
  
          if fuzz.ratio(self.string, other.string) &gt; 90: # fuzzywuzzy library checks if strings are "similar enough"
              return True
          else:
              return False
  </code></pre>
  
  <p>Now what I want to achieve is to check which objects in list a are "in" list b (return true against == when compared to some object in list b).</p>
  
  <p>Currently I'm just looping through them as follows:</p>
  
  <pre><code>for obj in a:
      for other_obj in b:
          if a == b:
              &lt;do something&gt;
              break
  </code></pre>
  
  <p>I strongly suspect that there is a faster way of implementing this. The lists are long. Up to like 100 000 objects each. So this is a big bottleneck in my code.</p>
  
  <p>I looked at this answer <a href="https://stackoverflow.com/questions/5993621/fastest-way-to-search-a-list-in-python">Fastest way to search a list in python</a> and it suggests that sets work much better. I'm a bit confused by this though:</p>
  
  <ul>
  <li><p>How significant is the "removal of duplicates" speedup? I don't expect to have many duplicates in my lists.</p></li>
  <li><p>Can sets remove duplicates and properly hash when I have defined the <strong>eq</strong> the way I have?</p></li>
  <li>How would this compare with pre-ordering the list, and using something like binary search? A set is unordered...</li>
  </ul>
  
  <p>So what is the best approach here? Please provide implementation guidelines in the answer as well.</p>