Variables are fundamental components of a program to store and work with single values. Sometimes we have multiple values or lists of information that we need to work with. In Python, we can use a list to store multiple values. Using a list in our programs allows us to use one variable to refer to multiple values and easily search for information in the list and perform calculations. Let's take a look at how to use Python lists. To learn more about Python with live expert instruction, enroll your child in an online free Python class for kids.

How To Use Python Lists: How To Store A List In Python

In Python, we create a list by placing the values we want to store inside square brackets [ ]. Each value is separated by a comma. Each value in a list is often referred to as an element.

Python list element

A list can have as many items as you want, and it can store integers, decimal values, strings, or objects. Each element is stored at an index in the list. In Python, the indexes start at 0. So this list has five elements, and the elements are at indexes 0 to 4.

There are several ways that we can access the elements in a list.

We can use square brackets [ ] and the index where the element is located to access the value.

Input for Python list

Output:

Output in Python list

Lists are mutable, which means their elements can be changed. We can use the assignment operator = to change a value in the list.

Mutable Python lists

Output:

Lists are objects, which means they also have functions we can use to work with lists. For example, we can use the append() function to add an item to the list. The append() function will add the given value to the end of the list.

Python lists are objects

Output:

Output

We can also use the insert() function to insert an item at a specific index in the list. We first specify the index where we want to place the value then the value to insert at that location.

Use Insert Python Function

Output:

Python function example for a list

If we need to remove an item from the list, we can use the remove() function. The remove() function will remove the first occurrence of the given value from the list and shifts the rest of the list forward.

How to remove a value from a list in Python

Output:

Python function example

Now that we have data stored in a list, we can use loops to iterate through each item in the list. Let's print each value that is in the list using a for loop. We write the for loop to access each item in the list grades and print each item.

Python For Loop

Learn more about for loops in Python.

Using loops and lists together, we can search for specific values in the list. For example, let's find and print any values that are greater than 90. Inside the for loop, we can use an if statement to check if the item is greater than 90. If this is true, then we will print the item. If this is false, we will do nothing.

Python list

Output:

We can also perform calculations, such as finding the average of the values in the list. To do this, we start by creating a variable called sum to store the total of all the values in the list. We then iterate through the list and add each value to sum.

To find the average, we need to divide sum by the total number of values in the list. Python has a len() function that returns the number of items in a list. We can use this to divide sum by the length of the grades list and print the result.

Print the result of a Python list

Output:

Result

Real-World Applications Of Python Lists

Python lists are more than just a way to store data—they're incredibly versatile tools used in many real-world applications that your child might find fascinating. Here’s how Python lists come into play:

  1. Managing Inventory in E-Commerce: In online shopping platforms, Python lists are often used to manage and track inventory. Each product can be stored as an item in a list, allowing the system to easily add, remove, or update products as stock levels change. For instance, when a customer makes a purchase, the system updates the list by reducing the quantity of the purchased item.
  2. Organizing and Analyzing Data: Python lists are crucial in data analysis, where large datasets are stored in lists for easy manipulation. For example, in a research project, a list might contain temperature readings over a month. Analysts can use lists to calculate averages, detect trends, or visualize data, making complex information more accessible and actionable.
  3. Creating and Managing To-Do Lists: Lists in Python can be used to automate the management of to-do lists. Whether it’s a daily planner or a project management tool, Python lists can store tasks, check them off when completed, and even prioritize them. This makes list management efficient and dynamic, allowing for easy updates and customizations.
  4. Building Recommendation Systems: Python lists are foundational in creating recommendation systems, like those used by streaming services or online stores. These systems often use lists to store user preferences and viewing or purchase history. By analyzing these lists, the system can suggest new movies, shows, or products that align with the user's interests, enhancing the user experience.
  5. Handling User Input in Web Development: In web development, Python lists can be used to collect and manage user input. For example, when filling out an online form, each piece of data (like name, email, and address) can be stored in a list. This data is then easily accessible for processing, validation, or sending to a database.

Python Lists Coding Challenges For Kids

After mastering the basics of Python lists, it's time to dive into some exciting coding challenges that will help your child practice and strengthen their skills. Here are a few fun and progressively challenging exercises to get them started:

1. Shopping List Organizer

Create a program that allows the user to input items they want to buy and store them in a list. Once the list is complete, use Python to sort the items alphabetically and display the organized shopping list. This challenge teaches how to work with user input, lists, and sorting methods.

2. Find the Maximum Number

Write a program that takes a list of numbers as input and finds the largest number in the list. This exercise helps kids practice iterating over a list and using conditional statements to compare values.

3. Remove Duplicates

Challenge your child to write a program that removes duplicate items from a list. For example, if the list contains ['apple, 'banana', 'apple', 'orange', 'banana], the program should return ['apple', 'banana', 'orange']. This task reinforces the concept of list manipulation and how to work with sets to ensure uniqueness.

4. Reverse a List

Ask your child to create a program that reverses the order of elements in a list. For instance, if the original list is [1, 2, 3, 4, 5], the program should output [5, 4, 3, 2, 1]. This challenge helps kids understand list indexing and how to manipulate list elements.

5. Average Calculator

Write a program that calculates the average of all numbers in a list. This exercise encourages kids to iterate over a list, sum the values, and then divide by the total number of elements to find the average.

6. List Concatenation

Challenge your child to merge two lists into one. For example, if you have list1 = [1, 2, 3] and list2 = [4, 5, 6] the program should combine them into list3 = [1, 2, 3, 4, 5, 6]. This exercise helps kids understand how to use list operations and extend their knowledge of list manipulation.

Try Using Python Lists

Lists make it easier for us to work with multiple related values in our programs. Lists help us organize information and make it easy to search for information and perform calculations. Empower your child to learn more about the different ways we can use lists in our live online Python for AI course or Python Camps, designed by experts from Google and MIT. Your child can even earn a certificate!

Written by Jamila Cocchiola who has always been fascinated with technology and its impact on the world. The technologies that emerged while she was in high school showed her all the ways software could be used to connect people, so she learned how to code so she could make her own! She went on to make a career out of developing software and apps before deciding to become a teacher to help students see the importance, benefits, and fun of computer science.