In the previous article, you were introduced to the idea of data structures and why programs need them. While variables allow programs to store individual values, real-world applications usually work with groups of related data.
A program might need to store a list of user names, a collection of product prices, or a sequence of records from a dataset. Managing these values as separate variables would quickly become impractical.
Data structures solve this problem by organizing multiple values inside a single structure that can be accessed and processed efficiently.
One of the most commonly used data structures in Python is the list.
What Is a List?
A list is an ordered collection of values stored in a single object. Lists allow programs to group related data together and process it as a sequence.
For example:
numbers = [10, 20, 30, 40]Here, the variable numbers refers to a list containing four values. Each element is stored in a specific position within the list.
Lists are particularly useful when a program needs to handle multiple items of the same type, such as a group of numbers, a collection of names, or a sequence of results produced by a computation.
Because lists maintain order, each element can be accessed using its position in the sequence.
Accessing Elements in a List
Every element in a list has an index that represents its position. Python uses zero-based indexing, which means the first element has index 0.
For example:
numbers = [10, 20, 30, 40]
print(numbers[0])
print(numbers[2])The first statement prints the value 10, while the second prints 30.
Indexes allow programs to retrieve or modify specific elements within the collection.
Negative indexes can also be used to access elements from the end of the list:
print(numbers[-1])This returns the last element of the list.
Modifying Lists
Lists are mutable, which means their contents can be changed after they are created.
For example:
numbers = [10, 20, 30]
numbers[1] = 50
print(numbers)Here the value at index 1 is replaced with 50.
Programs can also add new elements to a list using the append() method:
numbers.append(40)This adds a new element to the end of the list.
Similarly, elements can be removed using methods such as remove() or pop().
This flexibility makes lists suitable for situations where the collection of data changes during program execution.
Iterating Through Lists
Lists are often processed using loops, especially when a program needs to perform the same operation on every element.
For example:
numbers = [10, 20, 30, 40]
for number in numbers:
print(number)In this loop, the variable number refers to each element of the list in turn.
This pattern allows programs to process entire collections of data efficiently without manually accessing each element.
List Methods and Built-in Functions
Python provides many built-in operations for working with lists.
Some commonly used list methods include:
append()– adds an element to the end of the listinsert()– adds an element at a specific positionremove()– removes a specific valuepop()– removes an element by indexsort()– sorts the elements of the listreverse()– reverses the order of elements
Lists also work well with built-in functions such as len(), sum(), min(), and max().
For example:
numbers = [5, 10, 15]
print(len(numbers))
print(sum(numbers))These operations make lists powerful tools for managing collections of data.
Lists in Real Programs
Lists appear in almost every type of Python program. They are used to store groups of related data that need to be processed together.
- A web application may use lists to store search results.
- A data analysis script may process lists of measurements.
- An automation tool might maintain a list of tasks that need to be completed.
Because lists preserve order and support modification, they are extremely versatile and suitable for many programming tasks.
Why Lists Matter
Lists are one of the most fundamental data structures in Python. They provide a flexible way to store and manipulate ordered collections of values.
By combining lists with loops, conditional statements, and functions, programs can process large amounts of data efficiently. Understanding lists is therefore a crucial step toward building more advanced programs and working with structured data.
What Comes Next
Now that you understand how lists store ordered collections of values, the next step is to explore another important data structure.
In the next article, you will learn about Tuples in Python, which are similar to lists but represent fixed sequences of data that cannot be modified after creation. Because while some collections need to change during execution, others are designed to remain constant.