A list in Python is exactly what you’d expect - it’s a list of values separated by commas. Here’s how you declare a list in Python:

listname = [value1, value2, value3]What if you want to get a value from your list? Each value in a Python list has an index - a value that tells what position it is at in the list. The first element has an index of zero, the second element is at index 1, the third element is at index 2, and so on. To get a value from your list, you use its index:​

listname[index]

What happened in the last example? Remember, my_list was an empty list, which means it doesn’t have any values. There is nothing at index 0, so Python returns an error. Remember to check if your index is in the list when you write code! To get the last item in a list, you can also use the index -1.

2. List methodsPython lists have many methods you can use for things like getting the length of the list, adding an item to the list and removing an item from the list. ​len(my_list) Returns the length of your list

my_list.append(value)Adds the value in parentheses onto the end of your list

my_list.remove(value)Finds and removes the value in parentheses from the list

3. Manipulating and Slicing ListsIf you want to change a value in you list, you can set it equal to something else using its index. For example, if you have a list of people’s ages and you’ve made a mistake by typing in someone’s age as 333 instead of 33:

What if you want to access a subset of the list, instead of just a value? You can slice the list over a range of indices. For example, if you want all ages from the first to the fourth age, you would slice ages like this:ages[0:4]The notation in the brackets means that slicing starts at index zero and cuts the list just before index four. Because it’s 0, you can also write the slice as ages[:4] and the 0 will be implied. Additionally, if you’re slicing to the last index, you can leave it blank and it will be implied (ages[2:] slices from the third element to the end of the list). Here’s some more examples of slicing:​

REMEMBER: slicing includes the starting index, but excludes the ending index!4. Using Loops with ListsLoops can be extremely useful when working with lists - you could loop over all the elements in a list, for example. Here’s an example of a for loop that sums up monthly precipitation values: (tip: remember that += simply means to add the two values and set the sum equal to the variable on the left)​

range(len(monthly_rain)) simply creates a range of numbers from 0 to the length of our list - in other, words i is each index in our list. We then add the value at each index in the list to our rainfall_sum. Although this is entirely correct, Python offers much cleaner syntax for looping through the items in a list: for value in list:rainfall_sum += month“value” can be any variable name - it just represents the current value of the list we are at. Using this syntax, our rainfall code would become: