title: Get status code of url efficiently in python, alternative for for-loop
body: <p>I want to check a list of urls (in a column of a dataframe df) for their status codes (404, 403 and 200 seem to be the interesting ones). I defined a function which does the job. However, it uses a for-loop which is inefficient (I have a long list of urls!).</p>
  
  <p>Does anyone have a hint on how to do it more efficiently? Optimally the returned status code would also be displayed in a new column of the dataframe, e.g. df['status_code_url']. </p>
  
  <pre><code>def url_access(df, column):
      e_404 =0
      e_403 =0
      e_200 =0
      for i in range(0, len(df)):
          if requests.head(df[column][i]).status_code == 404:
              e_404= e_404+1
          elif requests.head(df[column][i]).status_code == 403:
              e_403 = e_403 +1
          elif requests.head(df[column][i]).status_code == 200:
              e_200 = e_200 +1
          else:
              print(requests.head(df[column][i]).status_code)
  
      return ("Statistics about " + column , '{:.1%}'.format(e_404/len(df)) 
              + " of links to intagram post return 404", '{:.1%}'.format(e_403/len(df)) 
              + " of links to intagram post return 403", '{:.1%}'.format(e_200/len(df)) 
              + " of links to intagram post return 200")
  </code></pre>
  
  <p>Thank you a lot!</p>