title: How to print the average, min and max for a specific year from a CSV file?
body: <p>I need help with this exercise. I have a csv file like this one in one column but with 16000 entries:</p>
  <p>Entity,Code,Year,Life expectancy (years)<br />
  Afghanistan,AFG,1950,27.638<br />
  Afghanistan,AFG,1951,27.878<br />
  Serbia,SRB,1995,71.841<br />
  Zimbabwe,ZWE,2019,61.49</p>
  <p>I need to print this, I got the first 2 parts working.</p>
  <ol>
  <li>✅What is the year and country that has the lowest life expectancy in the dataset?</li>
  <li>✅What is the year and country that has the highest life expectancy in the dataset?</li>
  <li>❌Allow the user to type in a year, then, find the average life expectancy for that year. Then find the country with the minimum and the one with the maximum life expectancy for that year.</li>
  </ol>
  <p>So far I´m here and I need some help on how to get the last part to print something like this related to the year input by the user:</p>
  <p><strong>For the year 1959:</strong></p>
  <p><strong>The average life expectancy across all countries was 54.95</strong></p>
  <p><strong>The max life expectancy was in Norway with 73.49</strong></p>
  <p><strong>The min life expectancy was in Mali with 28.077</strong></p>
  <pre><code>import csv
  print (&quot;Enter a year to find the average life expectancy for that year: &quot;)
  input_year = input (&quot;\n&quot;&quot;YEAR: &quot;)
  
  
  #Allow the user to type in a year, then, find the average life expectancy for that year
  def subset_year(all_data, selected_year):
      year_only = []
      for entity, code, year, expectancy in all_data:
          if year == selected_year:
              year_only.append((entity, code, year, expectancy))
      return year_only
  
  def pr_row(headers, row):
      return &quot;, &quot;.join(f&quot;{label}:{value}&quot; for label, value in zip(headers, row))
  
  data = []
  with open(r&quot;C:\Users\X230\Desktop\Python class\life-expectancy.csv&quot;) as database:
      reader = csv.reader(database)
      # the first row in the CSV file is; Entity,Code,Year,Life expectancy (years)
      # example of the data in the CVS file: Afghanistan,AFG,1950,27.638
      parts = next(reader)
      for line in reader:
          # print(line) #this prints everything, not very useful so I removed it
          # Save the parts I need into variables
          entity, code, year, expectancy = line
          data.append([entity, code, int(year), float(expectancy)])
  
  def key_year(row):
      return row[3]
  print()
  print(&quot;The overall max life expectancy is: &quot;, pr_row(parts, max(data, key=key_year)))
  print(&quot;The overall min life expectancy is: &quot;, pr_row(parts, min(data, key=key_year)))
  print(&quot;The average life expectancy across all countries was: &quot;, ) #??????????????
  print(&quot;The max life expectancy was in: &quot;, ) #????????????????????????????????????
  print(&quot;The min life expectancy was in: &quot;, ) #????????????????????????????????????
  year = input_year
  all_by_year = subset_year(data, year)
  print(all_by_year)
  </code></pre>