Kurssisisältö
Introduction to Python (copy)
Introduction to Python (copy)
Lists and List Methods
Welcome to the exciting world of Python lists! In this chapter, we dive into how lists serve as a versatile tool for managing collections of items, essential for scenarios like organizing product inventories in a grocery store. Through practical applications and video demonstrations, you'll learn to create, manipulate, and apply list methods effectively.
Watch as Alex demonstrates how to create and manipulate lists in our grocery store case.
Fundamentals of Lists
Lists in Python are highly flexible, capable of storing a diverse range of objects, including numbers, strings, and even other lists.
Here's how they work:
Creation
Lists can be created by enclosing comma-separated values in square brackets []
. You can also convert iterable objects (like strings, sets, tuples) into lists using the list()
constructor.
Ordering
The elements in a list maintain a specific order, which does not change unless explicitly modified using list methods (more on list methods later!).
Mutability (Changeability)
Lists are changeable, allowing you to add, remove, or alter elements after the list has been created.
Allowing Duplicates
Since each element in a list is indexed, the same value can appear multiple times at different positions.
Examples
Here's a simple example of a list containing different types of grocery items:
# A list showcasing various grocery categories grocery_items = ["milk", "eggs", "cheese", "butter"] print(grocery_items)
Similar to string indexing, elements inside of a list can also be accessed using index numbers:
Lists also have some flexibility since they are not restricted to a single data type. You can store a combination of different types of data within the same list. You can even store lists within a list:
# A list containing information about the apple category # Each position holds different apple details: # Name, quantity, discount status, price, and origin apple_details = ["apple", 34, True, 1.99, "Fuji"] print(apple_details)
List Methods
Python provides several methods that you can use to manipulate lists. These methods make it easy to modify, search, and manage lists effectively.
Let's explore some of the most commonly used methods:
append()
: adds an item to the end of the list;remove()
: removes the first occurrence of an item from the list;sort()
: sorts the items of the list in ascending (or descending) order.
Note
To use list methods in Python, you must invoke them on a list object using dot notation. This involves appending the method name to the list name followed by parentheses, as shown here:
list_name.append("new element")
.
The next example will demonstrate how to apply various list methods using dot notation.
Imagine you need to update your store's inventory by adding new items and removing outdated ones.
Here's how you can do it using list methods:
# Creating an inventory inventory = ["carrots", "bananas", "apples"] # Adding a new item inventory.append("oranges") # Removing an outdated item inventory.remove("bananas") # Sorting the inventory inventory.sort() # Checking the result print("Updated inventory:", inventory)
1. What will be the output of the following Python code?
2. Consider the list items = ["bread", "milk", "eggs", "yogurt"]
. Which line of code would correctly remove "milk"
from the list?
Kiitos palautteestasi!