Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lists and List Methods | Other Data Types
Introduction to Python Video Course
course content

Conteúdo do Curso

Introduction to Python Video Course

Introduction to Python Video Course

1. Getting Started
2. Variables and Types
3. Conditional Statements
4. Other Data Types
5. Loops
6. Functions

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:

123
# A list showcasing various grocery categories grocery_items = ["milk", "eggs", "cheese", "butter"] print(grocery_items)
copy

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:

12345
# 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)
copy

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:

1234567891011121314
# 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)
copy
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?

What will be the output of the following Python code?

Selecione a resposta correta

Consider the list items = ["bread", "milk", "eggs", "yogurt"]. Which line of code would correctly remove "milk" from the list?

Selecione a resposta correta

Tudo estava claro?

Seção 4. Capítulo 1
We're sorry to hear that something went wrong. What happened?
some-alt