Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn List Methods | Other Data Types
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Introduction to Python

bookList Methods

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
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
Task

Swipe to start coding

You are managing a grocery store's inventory. Use the correct list methods to update the inventory as described below:

  • Add "apples" to the inventory list;
  • Remove "bread" from the inventory list;
  • Sort the inventory list in alphabetical order.

Make sure to use the appropriate list methods for each step. Do not create a new list; modify the existing inventory list directly.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 2
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

bookList Methods

Swipe to show menu

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
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
Task

Swipe to start coding

You are managing a grocery store's inventory. Use the correct list methods to update the inventory as described below:

  • Add "apples" to the inventory list;
  • Remove "bread" from the inventory list;
  • Sort the inventory list in alphabetical order.

Make sure to use the appropriate list methods for each step. Do not create a new list; modify the existing inventory list directly.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 2
single

single

some-alt