Introduction:

In Python, a list is one of the most useful and commonly used data structures. A data structure is a way of organising and storing data in a computer so that it can be accessed and modified efficiently. Different programming languages have various data structures, such as arrays, which you may encounter in other languages like Java, C, or C++.

Arrays in Other Languages:

An array is a data structure used in many programming languages. It is:

  • Static: The size of the array is fixed when it’s created, meaning you cannot add or remove elements once the array is set.
  • Immutable: Once an array is created, you cannot modify the contents of it directly. If you need a different size or content, you’ll have to create a new array.

Lists in Python:

However, Python doesn't have arrays in the traditional sense. Instead, Python uses lists, which are like arrays but with some important differences:

  • Dynamic: Lists can grow and shrink in size as needed. You can add, remove, or change elements in a list freely.
  • Mutable: The contents of a list can be modified—whether you want to add, remove, or change individual items.

This makes lists highly flexible, allowing you to store multiple values in a single variable and manage them easily.

Creating Lists:

You can create a list in Python by using square brackets [] and placing your items inside, separated by commas.

Example:

This creates a list called favourite_fruits containing three items: 'Apple', 'Banana', and 'Cherry'. Lists can store different types of data, including numbers, strings, and even other lists!

 

Adding Items to a List:

You can add new items to a list using the append() method. This adds the item to the end of the list.

Example:

In this example, we start with three fruits in the list and then add 'Dragonfruit' to the end.

 

Removing Items from a List:

You can remove items from a list using the remove() method, which removes the first matching element. Alternatively, you can use pop() to remove an item by its index.

Example using remove():

Here, we remove 'Banana' from the list. The updated list now has 'Apple', 'Cherry', and 'Dragonfruit'.

Example using pop():

Here, pop() removes the last item ('Dragonfruit') and stores it in removed_item, so you can see what was removed.

 

Indexing in Lists:

To access specific items in a list, you can use indexing. Python lists are zero-indexed, meaning the first item is at index 0, the second item is at index 1, and so on.

Example:

In this example, favourite_fruits[0] accesses the first item ('Apple'), and favourite_fruits[1] accesses the second item ('Banana').

 

Conclusion:

Lists in Python are powerful because they allow you to store multiple items in a single variable, access and modify those items easily, and change the size of the list whenever you need to. Whether you’re keeping track of a to-do list, organising data, or building complex programs, lists will be a vital tool in your programming toolkit!

Predict:

Take a look at the following code. What do you think will happen when it runs? Try to predict how the program will behave.

friends = ["Alice", "Bob", "Charlie"]
print(friends[1])
friends.append("David")
print(friends) 

Think about what the code will do. How will it access and display the list items? What do you expect will happen after a new friend is added?

Run:

Now let’s run the code to see how lists work in Python. In this example, we’ll create a list, add an item, and access elements from the list.

Run the code and observe what happens. Can you see how the list changes after we add "David"? What does friends[1] return?

Investigate:

Now let’s look deeper into how we can manipulate lists. In Python, you can add or remove items from a list, and you can access specific elements using indexing.

Here’s another example that builds on the previous one.

Instructions: Run the code and investigate how the list changes. What happens when we remove the number 30? How does insert() work to add 15 at index 1?

Modify:

It’s time to modify some code! Below is an incomplete program that creates a list of favourite foods. The code needs to add a new food to the list and then print the final list.

Can you complete the code to add "Ice cream" to the list of favourite foods?

Hint: Use the append() method to add "Ice cream" to the list, then print the updated list.

Scroll to Top