Last Updated on October 23, 2024

As the preferred language for data analysis and manipulation, Python supports several compound data types, among them iterables and more specifically, sequences. 

A list is a sequenced data type — meaning it’s always ordered — and the most versatile one at that. In this article, we’ll define lists before exploring how to use them in Python.

What Are Lists in Python?

We use lists to store a collection of data in one variable. Well, you might think, don’t other data types do the same thing? What differentiates lists from tuples, sets and dictionaries? 

Lists are ordered and mutable (or changeable), while no other data type has this combination of qualities. Tuples are like lists in that they’re ordered and allow duplicates, but they’re immutable. By contrast, sets and dictionaries are both unordered and immutable, with dictionaries consisting of key-value pairs whereas sets are just a collection of unique values.

Python itself considers lists to be objects with the data type list:

Before we dive into how to use lists, let’s take a closer look at what “ordered” and “mutable” mean. Python uses something called an index to order list items —  the first element being the value stored at the 0th index, the second item at the 1st index and so forth. A list’s length is always the number of items it contains, so keep in mind that the last index of a list is its length minus one. To access an item, you may use square brackets containing the index of the element in question:

Because lists are mutable, we can add, change and remove items after they are declared. Much of the functionality we’ll explore in this article indeed centers on accessing list elements in order to change them.

Working With Lists

Lists, like most data types in Python, have built-in functions called methods available to them. Methods allow us to create lists and perform a range of operations on them. In this section, we’ll go over both special syntax and built-in functions we can use when working with lists.

Creating a List

To create a list in Python programming, you can place all the items you want — in the order you want — inside square brackets []. You may include any number of items and they can be of varying data types (e.g., integer, float, string, another list, etc.), but make sure to separate these with commas:

You could also opt to use the list() constructor with double round-brackets when creating a new list:

If you wanted to make a new list, where each item results from functions applied to an input list’s items, you’d create it with a list comprehension. For example, let’s say we want to double each element in a list of [0, 1, 2, 3, 4]. Here’s how we might do so by instantiating an array called doubles with square brackets and then using a for-loop:

Making our list in this manner comes with a side effect: We thereby create a variable x that remains after the loop’s completion. Generally, it’s better to avoid functionality with side effects unless these serve an intended purpose. We can use list comprehension to achieve our goal with no side effects:

Here, x no longer exists once our list is made. Apart from lacking side effects, this code is more transparent. We’re creating a variable called doubles and assigning it a list — denoted by the square brackets — composed of the product of 2 and each number up to 4 (the range starts at 0 and stops before the specified number).

Building a List

Now that we know how to create a list, let’s look at how to add on to an already-declared list. When we built doubles using the for-loop, we saw how it’s possible to add one item to a list using the append() method. The extend() method performs similarly, but allows multiple arguments:

We can also use + operator to combine two lists. This is called concatenation:

If you want to add an item at a specified index, you can use the insert() method in Python, which takes the index as the first argument and the value as the second:

If you don’t need to add an item in the middle of an established list and don’t want to use the append method, you could make use of the assignment operator (=):

We can use the same syntax to change an item; you only need to use the preexisting index of the list you’re modifying:

What if we accidentally added an item to our array or no longer need it? Let’s look at how to remove items and even clear an entire list. 

Deleting From a List

If you know which element you want to remove, you can supply it to the remove() method, which you’ll call on a given list:

You can also use the del keyword to delete one or multiple items from a list. You’ll need to supply the index of the unwanted items:

Our list is still not accurate, so you can use the del keyword and specify elements at multiple indexes using the slicing operator : symbol:

You can also use del to delete an entire list, as follows:

If you simply want to remove the last item in a list, you can use the pop() method, which also returns the item it removed. You could still use the pop() method if you have a different index in mind, and you’d only need to supply it as an argument:

Lastly, you can use the clear() method to empty an entire list, as follows:

Note that the list does still exist but is now empty. 

Other List Functionality

We’ve barely scratched the surface of what Python programming can do with lists! There are many other methods available to lists — popular ones include the len() method, which measures a list’s length, and the sort() and sorted() methods, which order values in a list alphabetically or numerically. You can also do more with lists than just apply methods to them: Since lists are iterables, you can loop through them and use them for data analysis purposes.

The Splat Operator

Sometimes, instead of using list methods, you’ll want to create your own function that works with lists in some way. When doing so, you might use a function that takes an unknown number of positional arguments to best suit your needs. You can accomplish creating such parameter placeholders by passing an asterisk (*), also called the splat operator, before your parameter name:

The above function takes a sequence of numbers and uses the built-in map() function to return a list of each input number’s square. Let’s supply this function with some prime numbers:

You can see that adding a fourth argument of 7 does not cause the program to error out. This is because Python knows the function will take an arbitrary number of arguments. 
But let’s say we have our primes nicely packed in a list. We want to be able to supply a single argument to our square() function, which we can already accomplish with the *numbers parameter. However, we want our argument to be a list instead of a number. Let’s see what happens when we try it:

We get a TypeError here because a list can’t be multiplied — only a number can! Other functions, such as one using the reduce() method, might output a copy of our input list instead of erroring out. Either way, that’s not the result we want. How could we fix this?

Because the square() function expects at least one number as an argument, we need to unpack our list data and then pass it to the function. Good news: We can use the splat operator that we employed to unpack an arbitrary number of arguments (which we called *numbers) to unpack a list as a function argument!

In this case, if we pass *primes into our function instead of primes, the Python program will unpack every item in our primes list. Those numbers will then be stored in a list called numbers, as specified in the square() definition.  The output would look like this:

Exactly what we want! 

You can also use the splat operator when working with other iterables — it works in exactly the same way with tuples, and with dictionaries you’ll just need to use a double asterisk (**).

Learn More

In this article, we discussed Python lists, showing you how to create, build and clear them. We briefly went over some useful list methods before turning to the splat operator. 

Learning how to use Python’s various data types is part of every programmer’s journey to mastering the language. By taking our Introduction to Python Programming course, you’ll become proficient in Python fundamentals, bringing you that much closer to your future as a Python programmer.

Start Learning