Lists and Mutable Sequences

You might have heard about lists before, but do you know what makes them so special? They’re mutable sequences! That means they can change their values over time. Let me show you how to create a list and make it dance like a monkey on crack.

To start: let’s create an empty list called “my_list”. You do this by putting square brackets around nothing, like so:

# Creates an empty list called "my_list"
my_list = []

# Prints the contents of "my_list", which is currently empty
print(my_list) # prints [ ]

Now that we have a blank canvas to work with, let’s add some items to our list. We can use the `append()` method to do this. This method adds an item at the end of the list. Let’s say you want to buy some groceries and need to make a shopping list:

# Create an empty list to store groceries
groceries = []

# Use the append() method to add items to the list
groceries.append("bananas") # adds "bananas" to the end of the list
groceries.append("apples") # adds "apples" to the end of the list
groceries.append("oranges") # adds "oranges" to the end of the list

# Print the list of groceries
print(groceries) # prints ["bananas", "apples", "oranges"]

But what if you change your mind and decide to buy some grapes instead of oranges? No problem! Lists are mutable, remember? Let’s replace the last item in our list with “grapes”:

# This script replaces the last item in a list with "grapes"

# Define a list of groceries
groceries = ["bananas", "apples", "oranges"]

# Use indexing to access the last item in the list and replace it with "grapes"
groceries[-1] = "grapes"

# Print the updated list
print(groceries) # prints ["bananas", "apples", "grapes"]

Wow! Our shopping list has changed right before our eyes. But what if you want to remove an item from the list altogether? You can use the `remove()` method for that:

# This script removes an item from a list using the `remove()` method.

# Create a list of groceries
groceries = ["apples", "bananas", "grapes"]

# Remove "bananas" from the list
groceries.remove("bananas")

# Print the updated list
print(groceries) # prints ["apples", "grapes"]

But be careful! If you try to remove an item that isn’t in your list, Python will throw a `ValueError`. So make sure the item is actually there before removing it.

Now slicing and dicing our lists like a pro. You can use indexes (the numbers inside square brackets) to access specific items in your list:

# Creating a list of fruits
fruits = ["bananas", "apples", "oranges"]

# Printing the second item in the list (index 1)
print(fruits[1]) # prints "apples"

# Note: Indexing in python starts at 0, so the first item in a list has an index of 0, second item has an index of 1, and so on.

# To remove an item from a list, we can use the remove() method. However, we need to make sure the item is actually in the list before removing it, otherwise we will get a ValueError.
# To check if an item is in a list, we can use the "in" keyword.
# For example, if we want to remove "oranges" from our list, we can do the following:
if "oranges" in fruits:
    fruits.remove("oranges")
    print(fruits) # prints ["bananas", "apples"]

# If we try to remove an item that is not in the list, we will get a ValueError.
# For example, if we try to remove "grapes" from our list, we will get an error:
if "grapes" in fruits:
    fruits.remove("grapes")
    print(fruits)
else:
    print("Grapes are not in the list.") # prints "Grapes are not in the list."

But what if you want to get a slice of the list instead? You can use slicing syntax, which is two indexes separated by a colon. The first index specifies where to start and the second (optional) index specifies where to stop:

# This script demonstrates how to use slicing syntax to get a slice of a list.

# First, we define a list of fruits.
fruits = ["apples", "oranges", "bananas", "grapes", "strawberries"]

# To get a slice of the list, we use the slicing syntax, which is two indexes separated by a colon.
# The first index specifies where to start and the second (optional) index specifies where to stop.
# In this case, we want to get a slice of the list starting at index 1 and ending at index 3 (not including index 3).
# This will print out the elements at index 1 and 2, which are "oranges" and "bananas".
print(fruits[1:3])

You can also use negative indexes if you want to count from the end of your list. For example, `-2` would get the second last item in your list:

# This script demonstrates how to use negative indexes to access items in a list.

# Create a list of fruits
fruits = ["apples", "bananas", "oranges", "grapes", "peaches"]

# Use negative indexes to access items from the end of the list
# The first number in the brackets represents the starting index, while the second number represents the ending index (not inclusive)
# In this case, we are starting at the last item in the list and ending at the third last item
# So the output will be the second last and third last items in the list, in that order
fruits[-1:-3] # prints ["oranges", "grapes"] (the 'p' in "apples" is not included because we stop at index -3)

And that’s it! You now know how to create, modify and slice lists in Python. Remember: they are mutable sequences, so be careful when working with them.

SICORPS