list
The built-in list
data type is a versatile and widely-used data structure in Python that allows for the storage and manipulation of ordered, mutable sequences of elements.
Lists can contain heterogeneous data types and support a wide array of operations, making them essential for organizing and processing data in Python:
>>> colors = ["red", "green", "blue"]
>>> colors[0]
'red'
list
Constructor
list(iterable)
Arguments
Argument | Description |
---|---|
iterable |
Any iterable (sequence, container, or iterator) to convert into a list. |
Return Value
- Returns a Python
list
object
list
Examples
Creating an empty instance:
>>> empty = []
>>> empty
[]
Creating instances using a literal:
>>> fruits = ["apple", "banana", "cherry"]
>>> fruits
['apple', 'banana', 'cherry']
Creating an instance using the class constructor:
>>> list((1, 2, 3))
[1, 2, 3]
Accessing values:
>>> fruits[1]
'banana'
Changing values:
>>> fruits[0] = "mango"
>>> fruits
['mango', 'banana', 'cherry']
Deleting values:
>>> del fruits[2]
['mango', 'banana']
list
Methods
Method | Description |
---|---|
.append() |
Adds an element to the end of the list. |
.extend() |
Extends the list by appending elements from an iterable. |
.insert() |
Inserts an element at a specified position. |
.remove() |
Removes the first occurrence of a value. |
.pop() |
Removes and returns the element at the specified position. |
.clear() |
Removes all elements from the list. |
.index() |
Returns the index of the first occurrence of a value. |
.count() |
Returns the number of occurrences of a value. |
.sort() |
Sorts the list in place. |
.reverse() |
Reverses the list in place. |
.copy() |
Returns a shallow copy of the list. |
list
Common Use Cases
The most common use cases for the list
include:
- Storing and organizing sequences of data
- Iterating over elements for processing
- Dynamically adding or removing elements
- Accessing elements by index
- Sorting and reversing collections
list
Real-World Example
Let’s consider a scenario where you need to manage a list of tasks in a to-do application. You can use a list to store the tasks, allowing users to add, remove, and prioritize tasks efficiently.
>>> tasks = []
>>> # Adding tasks
>>> tasks.append("Write report")
>>> tasks.append("Call John")
>>> # Prioritizing a task
>>> tasks.insert(0, "Pay bills")
>>> # Completing a task
>>> completed_task = tasks.pop(1)
>>> # Current list of tasks
>>> tasks
['Pay bills', 'Call John']
In this example, the list data type helps in managing and organizing tasks efficiently, allowing for easy addition, removal, and prioritization of tasks.
Related Resources
Tutorial
Python's list Data Type: A Deep Dive With Examples
In this tutorial, you'll dive deep into Python's lists. You'll learn how to create them, update their content, populate and grow them, and more. Along the way, you'll code practical examples that will help you strengthen your skills with this fundamental data type in Python.
For additional information on related topics, take a look at the following resources:
- Python's .append(): Add Items to Your Lists in Place (Tutorial)
- Custom Python Lists: Inheriting From list vs UserList (Tutorial)
- How to Use sorted() and .sort() in Python (Tutorial)
- Building Lists With Python's .append() (Course)
- Sorting Data With Python (Course)