If you are learning Python, lists are one of the first tools you will reach for again and again. They let you store many values in a single variable, keep them in order, and change them whenever you need to. Almost every real program uses lists in some form, from a simple shopping app to a data analysis script. In this guide, you will learn what lists are, how to work with them, and the methods that make them so powerful.
What is a list in Python
A list is a collection of items stored in a specific order. The items can be numbers, text, or even other lists, and they do not all have to be the same type. You create a list by placing values inside square brackets, separated by commas. For example, a list of fruits might contain three text values, while a list of scores might contain several numbers.
Two features make lists especially useful. First, they are ordered, which means each item has a fixed position. Second, they are mutable, meaning you can change their contents after creating them. This flexibility is what sets lists apart from some other data structures in Python.
Accessing items with indexes
Every item in a list has a position called an index. Python starts counting indexes at zero, so the first item is at index 0, the second at index 1, and so on. You access an item by writing the list name followed by the index in square brackets.
Python also supports negative indexes, which count from the end of the list. The last item is at index -1, the second to last at index -2, and so on. This is handy when you want the final element without knowing how long the list is.
- Index 0 refers to the first item.
- Index -1 refers to the last item.
- Trying to access an index that does not exist raises an error.
Slicing a list
Sometimes you want more than one item at a time. Slicing lets you take a portion of a list by specifying a start and an end position. The slice includes the start index but stops just before the end index. You can also leave out one of the numbers to slice from the beginning or all the way to the end.
Slicing does not change the original list; it returns a new list containing the selected items. This makes it a safe way to work with parts of your data without affecting the source.
Changing, adding, and removing items
Because lists are mutable, you can update them in many ways. To change a value, you assign a new value to a specific index. To grow a list, Python offers several methods, and to shrink it, there are matching options.
| Method | What it does |
|---|---|
| append() | Adds a single item to the end of the list |
| insert() | Adds an item at a specific position |
| extend() | Adds all items from another list |
| remove() | Deletes the first matching value |
| pop() | Removes an item by index and returns it |
Knowing which method to use keeps your code clear. For example, use append when you add items one at a time, and use extend when you want to join two lists together.
Looping through a list
One of the most common tasks is going through every item in a list. A for loop does this cleanly, giving you each element in turn so you can print it, test it, or use it in a calculation. Loops are where lists really shine, because they let you apply the same action to many values with just a few lines of code.
When you also need the position of each item, Python provides a helper that gives you both the index and the value as you loop, which is useful for numbered output or updates.
Useful list methods
Beyond adding and removing, lists come with methods that handle everyday needs:
- sort() arranges the items in ascending order, and can also sort in reverse.
- reverse() flips the order of the items.
- len() tells you how many items the list contains.
- count() reports how many times a value appears.
- index() finds the position of the first matching value.
These small tools save you from writing extra logic and make your programs easier to read.
List comprehensions
As you grow more comfortable, you will meet list comprehensions, a compact way to build a new list from an existing one. Instead of writing a full loop, you describe the result in a single line. For instance, you can create a list of squared numbers or filter out values that do not meet a condition. Comprehensions are concise and, once you understand the pattern, very readable.
Common mistakes to avoid
Beginners often run into a few predictable issues. Forgetting that indexes start at zero leads to off-by-one errors. Modifying a list while looping over it can produce surprising results. And confusing methods that change the list in place, such as sort, with those that return a new value can cause bugs. Being aware of these traps early will save you time later.
Conclusion
Lists are a foundation of Python programming, and mastering them opens the door to more advanced topics like dictionaries, loops, and data processing. With practice creating, accessing, and modifying lists, you will find them becoming second nature. If you want to keep building your skills, Cursa offers free programming courses that cover Python and other languages, guiding you step by step from the basics to more complex projects.



























