title: Find the year with the most number of people alive in Python
body: <p>Given a list of people with their birth and end years (all between <code>1900</code> and <code>2000</code>), find the year with the most number of people alive.</p>
  
  <p>Here is my somewhat brute-force solution:</p>
  
  <pre><code>def most_populated(population, single=True):
      years = dict()
      for person in population:
          for year in xrange(person[0], person[1]):
              if year in years:
                  years[year] += 1
              else:
                  years[year] = 0
      return max(years, key=years.get) if single else \
             [key for key, val in years.iteritems() if val == max(years.values())]
  
  print most_populated([(1920, 1939), (1911, 1944),
                        (1920, 1955), (1938, 1939)])
  print most_populated([(1920, 1939), (1911, 1944),
                        (1920, 1955), (1938, 1939), (1937, 1940)], False)
  </code></pre>
  
  <p>I'm trying to find a more efficient way to solve this problem in <code>Python</code>. Both - <code>readability</code> and <code>efficiency</code> counts. Moreover, for some reason my code won't print <code>[1938, 1939]</code> while it should.</p>
  
  <p><strong>Update</strong></p>
  
  <p>Input is a <code>list</code> of tuples, where first element of a tuple is a <code>year</code> when person was born, and second element of a <code>tuple</code> is the year of death.</p>
  
  <p><strong>Update 2</strong></p>
  
  <p>End year (2nd part of tuple) counts as well as a year of the person being alive (so If the person dies in <code>Sept 1939</code> (we don't care about the month), he is actually alive in 1939, at least part of it). That should fix the 1939' missing in results.</p>
  
  <p><strong>Best solution?</strong></p>
  
  <p>While readability counts in favor of <a href="https://stackoverflow.com/a/31522489/1554153">@joran-beasley</a>, for bigger input most efficient algorithm was provided by <a href="https://stackoverflow.com/a/31523813/1554153">@njzk2</a>. Thanks @hannes-ovrén for providing analysis in <a href="https://gist.github.com/hovren/bf5335ba45cd7eee811e" rel="noreferrer">IPython notebook on Gist</a></p>