title: US Census API - Get The Population of Every City in a State Using Python
body: <p>I'm having an issue getting the population of every city in a specific state. I do get the population of cities but if I sum the population in every city I don't get the same number as the population of the state.</p>
  
  <p>I got my <a href="http://api.census.gov/data/key_signup.html" rel="noreferrer">API Key</a> used the <a href="http://api.census.gov/data/2010/sf1/variables.html" rel="noreferrer">P0010001 variable</a> for total population used the FIPS 25 for the state of Massachusetts and requested the population by the <a href="http://api.census.gov/data/2010/sf1/geography.html" rel="noreferrer">geography level "place"</a> which I understand it to mean city.</p>
  
  <p>Here is the Python 3 code I used:</p>
  
  <pre><code>import urllib.request
  import ast
  
  
  class Census:
      def __init__(self, key):
          self.key = key
  
      def get(self, fields, geo, year=2010, dataset='sf1'):
          fields = [','.join(fields)]
          base_url = 'http://api.census.gov/data/%s/%s?key=%s&amp;get=' % (str(year), dataset, self.key)
          query = fields
          for item in geo:
              query.append(item)
          add_url = '&amp;'.join(query)
          url = base_url + add_url
          print(url)
          req = urllib.request.Request(url)
          response = urllib.request.urlopen(req)
          return response.read()
  
  c = Census('&lt;mykey&gt;')
  state = c.get(['P0010001'], ['for=state:25'])
  # url: http://api.census.gov/data/2010/sf1?key=&lt;mykey&gt;&amp;get=P0010001&amp;for=state:25
  county = c.get(['P0010001'], ['in=state:25', 'for=county:*'])
  # url: http://api.census.gov/data/2010/sf1?key=&lt;mykey&gt;&amp;get=P0010001&amp;in=state:25&amp;for=county:*
  city = c.get(['P0010001'], ['in=state:25', 'for=place:*'])
  # url: http://api.census.gov/data/2010/sf1?key=&lt;mykey&gt;&amp;get=P0010001&amp;in=state:25&amp;for=place:*
  
  # Cast result to list type
  state_result = ast.literal_eval(state.decode('utf8'))
  county_result = ast.literal_eval(county.decode('utf8'))
  city_result = ast.literal_eval(city.decode('utf8'))
  
  def count_pop_county():
      count = 0
      for item in county_result[1:]:
          count += int(item[0])
      return count
  
  def count_pop_city():
      count = 0
      for item in city_result[1:]:
          count += int(item[0])
      return count
  </code></pre>
  
  <p><strong>And here are the results:</strong></p>
  
  <pre><code>print(state)
  # b'[["P0010001","state"],\n["6547629","25"]]'
  
  print('Total state population:', state_result[1][0])
  # Total state population: 6547629
  
  print('Population in all counties', count_pop_county())
  # Population in all counties 6547629
  
  print('Population in all cities', count_pop_city())
  # Population in all cities 4615402
  </code></pre>
  
  <p>I'm reasonable sure that 'place' is the city e.g.</p>
  
  <pre><code># Get population of Boston (FIPS is 07000)
  boston = c.get(['P0010001'], ['in=state:25', 'for=place:07000'])
  print(boston)
  # b'[["P0010001","state","place"],\n["617594","25","07000"]]'
  </code></pre>
  
  <p>What am I doing wrong or misunderstanding? <strong>Why is the sum of populations by place not equal to the population of the state?</strong></p>
  
  <p><a href="http://api.census.gov/data/2010/sf1/geo.html" rel="noreferrer">List of example API calls</a></p>