Everything You Need to Know

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.

Related

10 Python Terms Beginner Coders Should Know

These ten terms will help you get acclimatized.

List Basics: Creating and Accessing Items

Create a List

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

        my_shopping_list = []
    

You can initialize lists with predefined items:

        my_shopping_list = ["milk", "bread", "sugar"]
    

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

        my_shopping_list = ["milk", 1, [1, 2, 3], True]
    

Direct Assignments

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

        my_shopping_list = ["milk", "bread", "sugar"]

my_shopping_list[0] = "coffee"
print(my_shopping_list)

my_shopping_list[2] = "bananas"
print(my_shopping_list)

A terminal window displays the output for Python code. It shows that direct assignment changes the contents of a list.

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:

        my_shopping_list = ["milk", "bread", "sugar"]
print(my_shopping_list[0])
print(my_shopping_list[1])
Python output displays the words milk and bread.

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

        my_shopping_list = ["milk", "bread", "sugar"]
print(my_shopping_list[-1])
print(my_shopping_list[-2])
Python output displays the words sugar and bread.

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:

        my_shopping_list = ["milk", "bread", "sugar"]

for item in my_shopping_list:
    print(item)

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.

Python output that prints all three items of a list.

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

        my_shopping_list = ["milk", "bread", "sugar"]

for item in my_shopping_list:
    print(item + "!")

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

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:

        my_shopping_list = ["milk", "bread", "sugar"]
print(my_shopping_list[0:2])
Python output displays the words milk and bread.-1

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:

        my_shopping_list = ["milk", "bread", "sugar"]
print(my_shopping_list[1:])
Python output displays the words bread and sugar.

This will return everything up to 2:

        my_shopping_list = ["milk", "bread", "sugar"]
print(my_shopping_list[:2])
Python output displays the words milk and bread.-2

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.

        my_shopping_list = ["milk", "bread", "sugar"]
print(my_shopping_list[-2:])
Python output displays the words bread and sugar.-1

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.

        my_shopping_list = ["milk", "bread", "sugar"]

my_shopping_list.append("bananas")
print(my_shopping_list)

my_shopping_list.append("cheese")
print(my_shopping_list)

Python output that prints a list twice across two lines. Each line has a new item added to the end of the list.

insert()

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

        my_shopping_list = ["milk", "bread", "sugar"]


my_shopping_list.insert(0, "bananas")
print(my_shopping_list)


my_shopping_list.insert(2, "cheese")
print(my_shopping_list)

Python output that prints a list twice across two lines. Each line has a new item inserted at index 0 and index 2, respectively.

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 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.

        my_shopping_list = ["milk", "bread", "sugar"]


my_shopping_list.remove("milk")
print(my_shopping_list)


my_shopping_list.remove("sugar")
print(my_shopping_list)

Python output that prints a list twice across two lines. Each line has a specified item removed.

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.

        my_shopping_list = ["milk", "bread", "sugar"]

my_shopping_list.sort()
print(my_shopping_list)

Python output that prints a single list, sorted alphabetically.-1

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.

The sorted method is out-of-place, and it does not change the original list.

        my_shopping_list = ["milk", "bread", "sugar"]

my_sorted_list = sorted(my_shopping_list)
print(my_shopping_list)
print(my_sorted_list)

Python output that prints two lists. The first list is not sorted, but the second list is sorted alphabetically.

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.

        my_shopping_list = ["milk", "bread", "sugar"]

if len(my_shopping_list) > 0:
    print("The list is not empty.")

Python output that displays a message that states that the list is not empty.

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).

        if []:
    print("An empty list is truthy.")

if ["foo"]:
    print("A populated list is truthy.")

Python output that displays one message to confirm that a populated list is truthy.

This is a common way to check if a list has any items.

Related

Learn the Basics of Python in 1 Hour With These 13 Steps

Welcome to the world of Python!

Comparing Lists

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

  • 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.

        one = [1]
two = [2]
also_one = [1]

if one == two:
    print("Different lists are equal.")

if one == one:
    print("A list is equal to itself.")

if one == also_one:
    print("Different lists with the same items are equal.")

Python output that displays two messages, and both confirm that lists with identical members are equal to each other.

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:

        one = []
two = []

if one is two:
    print("Different lists have the same identity.")

if one is one:
    print("A list has the same identity as itself.")

Python output that displays one message that confirms that a list can only be identical with itself.

List Membership

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

        my_shopping_list = ["milk", "bread", "sugar"]

if "milk" in my_shopping_list:
    print("Got milk!")

if "cookies" in my_shopping_list:
    print("Got cookies!")

Python output that displays one message that confirms that a milk is in the shopping list.

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.

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

        my_shopping_list = ["milk", "bread", "sugar"]

def apply_uppercase(item):
    return item.upper()

map_object = map(apply_uppercase, my_shopping_list)
mapped_list = list(map_object)

print(mapped_list)

Python output that shows a list of uppercase words.

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.

        my_shopping_list = ["milk", "bread", "sugar"]

def sugar_free(item):
    
    return item != "sugar" 

filter_object = filter(sugar_free, my_shopping_list)
mapped_list = list(filter_object)

print(mapped_list)

Python output that shows a list missing one filtered value.

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).

        my_shopping_list = ["milk", "bread", "sugar"]


copied_list = [item for item in my_shopping_list]
print(copied_list)


changed_list = [item.upper() for item in my_shopping_list]
print(changed_list)
  

Python output that shows two lists: one with lowercase strings and the other with uppercase strings.

The second list comprehension is the same as:

        my_shopping_list = ["milk", "bread", "sugar"]


changed_list = []
for item in my_shopping_list:
    changed_list.append(item.upper())
    print(changed_list)
  

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.

  • 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:

        my_shopping_list = ["milk", "bread", "sugar"]
milk = my_shopping_list[0]
bread = my_shopping_list[1]
sugar = my_shopping_list[2]

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

        my_shopping_list = ["milk", "bread", "sugar"]

milk, bread, sugar = my_shopping_list
print(milk, bread, sugar)

A screenshot of Python output that displays the words milk, bread, and sugar.

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:

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

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:

        my_shopping_list = ["milk", "bread", "butter", "bananas", "sugar"]

milk, bread, *misc, sugar = my_shopping_list
print(milk, bread, misc, sugar)

Python output that shows unpacked items. The first and second items are strings, the third is a list with two strings, and the fourth is a string.

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:

        
milk_and_bread = ["milk", "bread"]


list_a = ["list_a", milk_and_bread]
list_b = ["list_b", milk_and_bread]


print("BEFORE CHANGE")
print(list_a)
print(list_b)


milk_and_bread[0] = "apples"


print("\nAFTER CHANGE")
print(list_a)
print(list_b)

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:

        from copy import deepcopy

milk_and_bread = ["milk", "bread"]


list_a = deepcopy(["list_a", milk_and_bread])
list_b = deepcopy(["list_b", milk_and_bread])


print("BEFORE CHANGE")
print(list_a)
print(list_b)


milk_and_bread[0] = "apples"


print("\nAFTER CHANGE")
print(list_a)
print(list_b)

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.

Related

Don’t Make This Mistake When You Start Your Python Project

Avoid dependency chaos with this simple trick.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top