title: Contains() function `in` in if condition in python
body: <p>I did some research about functionally of contains() <code>in</code> especially in comparison with eq() <code>==</code> and found out that it can perform many tasks. I managed to answer many of them (see below). 
  Is there any other useful usage of <code>in</code> except those below, for example with objects?</p>
  
  <p>I am also curious about situation mentioned in <a href="https://stackoverflow.com/questions/8705378/pythons-in-set-operator">Python&#39;s &quot;in&quot; set operator</a> that <code>b in s</code> mean that there is an element x of the set s such that x == b and hash(x) == hash(b). How can it be otherwise? Is there an example where both are not equal?</p>
  
  <p><strong>Research about in:</strong>
  <code>in</code> produces the same result in case of comparison of a string of the length of 1.</p>
  
  <pre><code>data = ['2','4','1','3']
  
  for d in data:
   if '1' in d:
    print(d)
    print(data.index(d))
  
  for d in data:
   if d in '1':
    print(d)
    print(data.index(d))
  
  for d in data:
   if '1' == d:
    print(d)
    print(data.index(d))
  </code></pre>
  
  <p>all 3 produces following result:</p>
  
  <pre><code>1
  2
  </code></pre>
  
  <p>Though here the similarity ends. <code>In</code> can be used for broad range of other comparisons:</p>
  
  <pre><code>data = [['1','2'],'4','1','3']
  
  for d in data:
   if '1' in d:
    print(d)
     print(data.index(d))
  
  &gt;&gt; ['1', '2']
  &gt;&gt; 0
  &gt;&gt; 1
  &gt;&gt; 2
  </code></pre>
  
  <p>But in this case it is order sensitive:</p>
  
  <pre><code>for d in data:
   if d in '1':
    print(d)
    print(data.index(d))
  
  &gt;&gt; TypeError: 'in &lt;string&gt;' requires string as left operand, not list
  </code></pre>
  
  <p>You can check the original list directly, but it works for sets, tuples, dict keys and strings. </p>
  
  <pre><code>data = ['3','2','1'] #string in list, work for numbers, lists etc.
  if '1' in data:
   print(data.index('1'))
  &gt;&gt; 2
  
  data = ['3',['2','1'],'0'] #string in list in list
  if '1' in data:
   print(data.index('1'))
  else:
   print('not found')
  &gt;&gt; not found
  
  data = ['3','x',['2','1'],'0'] #list in list
  if ['2','1'] in data:
   print(data.index(['2','1']))
  else:
   print('not found')
  &gt;&gt; 2
  
  data = ('3','2','1') #string in tuple
  if '1' in data:
   print(data.index('1'))
  &gt;&gt; 2
  
  data = set(['3','2','1']) #string in set
   if '1' in data:
   print('ok')
  &gt;&gt; ok
  
  data = {'1':'a','2':'b'} #string in dict keys
  if '1' in data:
   print(data['1'])
  &gt;&gt; a
  
  data = {'a':'1','b':'2'} #string dict values
  if '1' in data:
   print('ok')
  &gt;&gt;
  
  data = 'abc1efg' #string in string 
  if '1' in data:
   print(data.index('1')) 
  &gt;&gt; 3
  
  data = 'abc1efg' #number in string 
  if 1 in data:
   print(data.index(1)) 
  &gt;&gt; TypeError: 'in &lt;string&gt;' requires string as left operand, not int 
  
  data = [1,'x',(),{}] #dict in list
  if {} in data:
   print(data.index({}))
  &gt;&gt; 3
  </code></pre>
  
  <p>All of the above works with function <code>contains</code> from <code>operator</code> module.</p>
  
  <pre><code>import operator
  data = [1,'x',(),{}] #contains function
  if operator.contains(data,{}):
   print(data.index({}))
  &gt;&gt; 3
  </code></pre>