2) Data Structures Lesson

Python List

5 min to complete · By Jon Fincher

In Python, lists are the first basic data structure you learn about right after learning about variables. You can define exactly what you mean by a list.

What is a Python List

In Python, a list is a collection of values, usually of the same data type. You access the data values in a list using an index, which is an integer value that uniquely identifies each item in the array. In Python, indexes start with 0 -- that is, the first item in the array is at index 0.

Use Cases of a List

You can take a look at a practical use case for a list. Our simple example so far has been tracking high temperatures, but what if you wanted to track a range of weather-related data over the course of a month and output some analysis?

First, create a simple class to hold onto the data you want to track:

class Daily_Weather_Data:

  def __init__(self, high_temp, low_temp, rainfall, humidity, pressure, wind_speed, wind_direction):
    self.high_temp = high_temp
    self.low_temp = low_temp
    self.rainfall = rainfall
    self.high_humidity = humidity
    self.pressure = pressure
    self.wind_speed = wind_speed
    self.wind_direction = wind_direction

A list is used to store the data and gather the data you need. You can use a loop and input() to get data from the user:

weather_data = []

for i in range(30):
  print(f"Enter weather for day #{i}")
    print("---------------------------")
    high_temp      = int(input("High temperature: "))
    low_temp       = int(input("Low temperature: "))
    rainfall       = float(input("Rainfall in inches: "))
    humidity       = float(input("Humidity from 0.0 - 1.0: "))
    pressure       = float(input("Barometric pressure: "))
    wind_speed     = int(input("Wind Speed: "))
    wind_direction = int(input("Wind Direction in degrees: "))

    weatherData.append(Daily_Weather_Data(high_temp, low_temp, rainfall, humidity, pressure, wind_speed, wind_direction))

Of course, you could also read the daily weather data from a file or download it from a web API.

However you get your data, you can perform analysis on it using loops. For example:

def analyze_weather_data(weather_data):
  # You need to get the totals for different measurements
  total_high_temp=0;
  total_low_temp=0;
  total_wind_speed=0;
    
  total_rainfall=0.0;
  total_humidity=0.0;
  total_pressure=0.0;

  # Add them all up
  for i in range(len(weather_data)):
    total_high_temp  += weather_data[i].high_temp
    total_low_temp   += weather_data[i].low_temp
    total_rainfall   += weather_data[i].rainfall
    total_pressure   += weather_data[i].pressure
    total_humidity   += weather_data[i].humidity
    total_wind_speed += weather_data[i].wind_speed

    
    # Output some averages
  print(f"Average High      : {(total_high_temp / len(weatherData)}");
  print(f"Average Low       : {(total_low_temp / len(weatherData)}");
  print(f"Average Rainfall  : {(total_rainfall / len(weatherData)}");
  print(f"Average Humidity  : {(total_humidity / len(weatherData)}");
  print(f"Average Pressure  : {(total_pressure / len(weatherData)}");
  print(f"Average Wind Speed: {(total_wind_speed / len(weatherData)}");  
}

List Exercise

On your machine, modify the code above to read data from the weatherData.csv file, which contains high and low temperatures, rainfall, humidity, pressure, wind speed, and wind direction data for a given 30-day period. Then, output the following:

  • Average temperatures, pressure, rainfall, humidity, and wind speed over that period.
  • Maximum high and low temperatures, minimum high and low temperatures.
  • Highest daily rainfall.
  • Highest and lowest wind speeds.
  • Most common wind direction.

Summary: Lists

The list is the most basic data structure, storing a collection of values of the same data type and referenced by an index. List indexes start at 0, and all lists have a defined length, which can change over the life of the list. Loops work extremely well with lists to add, retrieve, delete, and operate on data in the list.