Yahoo
Skip to main content
Advertisement
Advertisement
Advertisement
Advertisement

Lists in Python: Everything You Need to Know

A laptop with the Python download webpage open on Chrome.
Hannah Stryker / How-To Geek

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, use my_shopping_list.append('milk'). There are many methods, and we will cover most of the important ones as we go.

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.

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:

The end numbers start at -1 , not 0.

Loop Over a List

A loop in 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.

Advertisement
Advertisement

It's possible to perform any action on each item . 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 called slicing . 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 saying zero, up to, but not including, two .

Remember that 0 and 1 are the first two items.

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:

Advertisement
Advertisement

Reverse Slicing

You can 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 goes to the end of the list.

Remember that end indexes don'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()

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.

Advertisement
Advertisement

Use the insert method 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 the remove method with a sample of what you want to delete, and it will delete the first instance that it encounters.

Sorting

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

  • In-place: Modifies the list.

  • Out-of-place: Returns a new list and doesn't modify the original list.

The sort method 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.

Advertisement
Advertisement

The sorted method 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 the len function.

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., if statements). 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 has any items.

Comparing Lists

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

Advertisement
Advertisement
  • Equality: Do the lists have the same contents ?

  • Identity: Are the lists the exact same object in memory?

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 the is operator to perform identity checks:

List Membership

You will often want to know if a list contains something, and you can do that with the special in operator.

Transforming Lists: Turning Items Into Something Else

Use map() to Change Items

Map is a common way to transform lists in most programming languages. In Python, map is 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.

Advertisement
Advertisement

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 like map , except it removes items from the list. The function receives each value, and it determines whether to keep or discard the item. Returning true means keep the value , and false means discard .

Bonus Tips: You'll Want to Read These

List Comprehensions

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

The second list comprehension is the same as:

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

A screenshot of Python code that relates the elements of a list comprehension, to a standard Python list.
Advertisement
Advertisement
  • Token 1: I've underlined it, and it relates to the other underlined token. This token is the variable that you operate on; it's this value that you change.

  • Token 2: refers to the variable declaration; this is where you assign a name to the token, which is "item" in this example.

  • Token 3: refers to the list that you loop (iterate) over.

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.

Advertisement
Advertisement

Take the following example:

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.

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:

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

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 as Great Games That Teach You Python or Basic but Useful Python Scripts to Get You Started . Lastly, our guide on writing code the Pythonic way includes additional topics about lists, including the all and every functions.

Advertisement
Advertisement
Mobilize your Website
View Site in Mobile | Classic
Share by: