Documentation isn’t beginner-friendly, and tutorials often leave gaps. Learning Python can feel overwhelming, so you’re not alone. To address this, I have prepared a concise, no-nonsense guide to rapidly boost your skills with Python lists—in less than 30 minutes too!

Lists are a sequence of items that live side-by-side in memory, like a shopping list on a piece of paper. Lists also provide operations (called methods) that allow you to perform actions on them. For example, to append an item to a list, usemy_shopping_list.append(‘milk’). There are many methods, and we will cover most of the important ones as we go.

Python output that prints all three items of a list.

List Basics: Creating and Accessing Items

Create a List

To create a list, simply assign square brackets to a variable:

You can initialize lists with predefined items:

You can store different types in lists, and even other lists:

Direct Assignments

When an existing list has items in it, we can change those items directly:

The number between square brackets is called the index, and we use it to address individual items in the list. The first item is always 0, then the second is 1, and so on.

Python output that prints all three items of a list, each with an exclamation point appended to the end.

Access List Items

Now that your list has items, you will want to access them:

Conversely, Python allows you to access items starting from the end using negative numbers:

A screenshot of Python code that relates the elements of a list comprehension, to a standard Python list.

Theend numbersstart at-1, not 0.

Loop Over a List

Aloopin general programming is a construct that repeats an action N times. For example, if a list contains three items, the loop will iterate through it, allowing us to perform actions on each item. The following is an example of a for loop:

For each item in “my_shopping_list,” the loop assigns it to the “item” variable. We can simply print that variable to show how it works.

A Python error that complains of an insufficient number of variables to unpack a list into.

It’s possible to perform any action on eachitem. For example, append an exclamation point to the item and then print it:

Retrieving Portions of a List (aka Slicing)

Retrieving portions of a list is calledslicing. Slicing doesn’t change the original list, it returns a new list with the desired items.

If you want to retrieve the first two items:

You can see that we provide indexes 0 and 2, separated by a colon—it’s the same as sayingzero, up to, but not including, two.

Remember that 0 and 1 are the first two items.

Python output displays two sets of two lists: before and after changes. The first set shows the original, unchanged lists. The second set shows that both lists have changed.

If you do not provide a number after or before the colon, it returns the rest of the list. For example, the following code returns the second item and the rest of the list:

This will return everything up to 2:

Reverse Slicing

you may perform slicing in reverse order by using negative numbers for indexes. Python processes negative indexes relative to the end of the list; for example, -1 means the last item, and -2 means the second to last item.

The following example starts at the second-to-last item (-2) and goestothe end of the list.

Python output displays two sets of two lists: before and after changes. None of the lists have changed.

Remember thatend indexesdon’t start from 0; they start from -1.

List Modification: Adding, Removing, and Sorting Items

To change the structure of a list (not its existing values), it’s easiest (and best practice) to use the standard methods that Python provides.

Append means to add to the end of a list.

insert()

Insert is like append, except it adds items to a specified index.

You can see that inserting at index 0 places it at the beginning, and inserting at index 2 places it at the third item.

Use theinsertmethod with care. For large lists (e.g., millions), it may impact performance, because Python moves each following item up one place to make room for the new item.

remove()

Provide theremovemethod with a sample of what you want to delete, and it will delete thefirstinstance that it encounters.

Sorting

Sorting a list in Python is simple, and you have two options:

Thesortmethod is in-place, and it changes the original list.

Notice that items are sorted alphabetically? Also notice that we did not reassign “my_shopping_list,” yet printing it showed a sorted list? This is in-place sorting.

Thesortedmethod is out-of-place, and it does not change the original list.

Notice that “my_shopping_list” is left untouched? The “sorted” method returns a new list: “my_sorted_list”.

Using List Information to Make Smart Decisions

Checking List Length

The most common list operation is probably checking the list length using thelenfunction.

As you can see, the list length is greater than or equal to zero.

Using Lists Conditionally

It’s possible to use lists in conditional statements (e.g.,ifstatements). An empty list is falsy (equivalent to false); a populated list is truthy (equivalent to true).

This is a common way to check if a list hasanyitems.

Comparing Lists

To compare lists, it is important to understand two key concepts:

Equality focuses on whether the lists contain the same elements; if they do, then the lists are considered equal. To check for equality, we use the “==” operator.

An identity check compares the memory addresses of two objects (lists) to determine whether they point to the exact same location in memory. We use theisoperator to perform identity checks:

List Membership

You will often want to know if a list contains something, and you’re able to do that with the specialinoperator.

Transforming Lists: Turning Items Into Something Else

Use map() to Change Items

Mapis a common way to transform lists in most programming languages. In Python,mapis a function that loops over a list and applies a function (that you provide) to each item. Your function receives each item as an argument, and you are free to do what you want with it. The value that you return replaces that item in the list.

In short: Map applies a user-provided function to each item. Use that function to transform each value.

Use filter() to Remove Items

Filter works much likemap, except it removes items from the list. The function receives each value, and it determines whether to keep or discard the item. Returning true meanskeep the value, and false meansdiscard.

List Comprehensions

List comprehensions are a compact way to generate a list from another list. It works much likemap(covered earlier).

Thesecondlist comprehension is the same as:

This image shows how a list comprehension relates to a normal Python list.

Unpacking Lists

Getting items from a list can be an ugly affair:

Instead, prefer list unpacking, a clean method for extracting each item into variables:

The positions of variable names relate to positions in the list, and the number of variables must match the number of items. Here is what happens when there are insufficient variables:

To remedy this, we can use the star operator (an asterisk) on an item. In this scenario, the starred variable represents multiple variables as a list—essentially, it’s everything else that isn’t explicitly assigned to a variable. Keep in mind that order matters:

Deep Copy

When you are using complex objects (class instances, lists, etc.), it’s important to understand they are not plain-old values. When you store an object in a variable, it’s actually a memory address that points to a place in memory where the object lives.

Take the following example:

Changes are reflected in both lists because “milk_and_bread” is an object, and Python always acts upon objects through their memory address (aka reference). When we add an object to a list, we are in fact adding a reference to the list instead. Now, when we change the object, the result is reflected everywhere.

The reason why this is important is that it can be a source of bugs. When you view complex values as references, instead of just variables, it’s easier to avoid this problem. It’s not always possible to avoid such a problem, and one solution is to deep copy the list:

A deep copy will drill down into every object, and copy all of them. It’s recursive.

The topics covered here should meet most of your needs for lists, but they do not cover the wider Python topic. To continue learning, you may find our other articles useful, such asGreat Games That Teach You PythonorBasic but Useful Python Scripts to Get You Started. Lastly, our guide onwriting code the Pythonic wayincludes additional topics about lists, including theallandeveryfunctions.