title: Creating method in Python
body: <p>I have a file. I am splitting it in a class. Also, I want to return top n years having the highest number of movie produced. And I will use lines attricute to get data.</p>
  <pre><code>import re
  
  import collections
  
   
  
  class movie_analyzer:
  
      def __init__(self,s):
  
              self.lines=open(s, encoding=&quot;latin-1&quot;).read().split('\n')
  
              self.lines=[x.split('::') for x in self.lines]
         
  
      def freq_by_year(self):
  
          movies_years = [x[3] for x in self.lines]
  
          c = collections.Counter(movies_years)      
  
          for movies_years, freq in c.most_common(3):
  
              print(movies_years, ':', freq)
  
  
  
  movie=movie_analyzer(&quot;modified.dat&quot;)
  
  movie.freq_by_year()
  </code></pre>
  <p>It gives this error:</p>
  <pre><code>---------------------------------------------------------------------------
  IndexError                                Traceback (most recent call last)
  &lt;ipython-input-627-51913258f9e4&gt; in &lt;module&gt;
  ----&gt; 1 movie.freq_by_year()
   
  &lt;ipython-input-624-8dc663c0b252&gt; in freq_by_year(self)
        9     def freq_by_year(self):
       10 
  ---&gt; 11         movies_years = [x[3] for x in self.lines]
       12 
       13         c = collections.Counter(movies_years)
   
  &lt;ipython-input-624-8dc663c0b252&gt; in &lt;listcomp&gt;(.0)
        9     def freq_by_year(self):
       10 
  ---&gt; 11         movies_years = [x[3] for x in self.lines]
       12 
       13         c = collections.Counter(movies_years)
   
  IndexError: list index out of range    
  </code></pre>
  <p>Also, movie.lines looks like this:</p>
  <pre><code>[['1', 'Toy Story', &quot;Animation|Children's|Comedy&quot;, '1995'],
   ['2', 'Jumanji', &quot;Adventure|Children's|Fantasy&quot;, '1995'],
   ['3', 'Grumpier Old Men', 'Comedy|Romance', '1995'],
   ['4', 'Waiting to Exhale', 'Comedy|Drama', '1995'],
   ['5', 'Father of the Bride Part II', 'Comedy', '1995'],
   ['6', 'Heat', 'Action|Crime|Thriller', '1995'],
   ['7', 'Sabrina', 'Comedy|Romance', '1995'],
   ['8', 'Tom and Huck', &quot;Adventure|Children's&quot;, '1995'],
   ['9', 'Sudden Death', 'Action', '1995'],
   ['10', 'GoldenEye', 'Action|Adventure|Thriller', '1995']]
  </code></pre>
  <p>.dat file looks like:</p>
  <p>Movies = [&quot;1::Toy Story::Animation|Children's|Comedy::1995\n&quot;,</p>
  <p>&quot;2::Jumanji::Adventure|Children's|Fantasy::1995\n&quot;,</p>
  <p>'3::Grumpier Old Men::Comedy|Romance::1995\n',</p>
  <p>'4::Waiting to Exhale::Comedy|Drama::1995\n',</p>
  <p>'5::Father of the Bride Part II::Comedy::1995\n']</p>