Exploring Lists in Python
In this lesson, we’ll explore lists a bit closer and cover the ways to create and use lists. This course is part of a series of content designed to help you learn to program with the Python programming language.
- Describe the syntax for creating lists
- Describe the concept of an index
- Describe the concept of a range
- Describe how to check a list for an object
- Describe how to return the length of a collection
This course was designed for first-time developers wanting to learn Python.
This is an introductory course and doesn’t require any prior programming knowledge.
Hello, and welcome! My name is Ben Lambert, and I’ll be your instructor for this course. This course is part of a series of content designed to help you learn to program with the Python programming language. Should you wish to ask me a specific question, you can do that with the contact details on screen. You can also reach support by using the email address: firstname.lastname@example.org. And one of our cloud experts will reply.
In this lesson, we’ll explore lists a bit closer. We’ll cover the ways to create and use lists. Lists objects are a container for storing other objects. Python provides a built-in function named list which can create a list object. When this function is called without providing any arguments it creates an empty list object.
However, lists are such commonly used data types that they’re integrated into Python language syntax. The syntax for creating a list is to use square brackets. For an empty list you use an open and close square bracket. To create a list with some objects you can add the objects separated by a comma.
Once you create a list object you can interact with it using its methods, operators, and with some additional language syntax. Let’s review some of the things that you can do with list objects. Let’s begin with an empty list. We’ll bind the name letters to the list object created by this list syntax.
Using the append method we can add some objects to the end of the list. Let’s add a few letters. Each of these strings will be appended to the end of the list. First a is appended. Then b is appended and becomes the end. Followed by the letter d.
If we print this list object, we’ll see that it includes the letters a b and d. In that order. Don’t worry about how it displays the contents of the list. For now, just accept that it does. Order is important to lists. By preserving the order of objects in the list, we can make safer assumptions about the behavior of our code. For example: imagine that if you want to display a list of scores in a particular order. Or determine the last user in a list.
In these cases, the order is important. Because order is important, each object in a list is given an index. The index begins with the number 0 and increases by one each time an object is added. The first object in the list is stored at index zero. The second is stored at index number one, and it keeps incrementing by one. Let’s investigate indexes by changing this list. Currently the letters in the list are a b and d. Let’s insert the letter c so that the order of the letters is: a b c d.
If we call the append method and provide the letter c as the argument it would add the letter to the end. So we need another method. This can be done using the insert method. The insert method can insert an object into a list at the specified index. In this case we can add the letter c to the second index. Which will result in the list containing: a b c d. We can access the objects inside a list based on the index. The syntax for that involves using square brackets and specifying the index we wish to access.
For example, accessing the letter at index zero is a. The letter at index 3 is d. If we attempt to access an index that doesn’t exist it will result in an error. In addition to accessing a single object we can also access a range of objects. The syntax for this is similar to single object-index access. Except it supports a starting and ending index. This syntax uses a colon to specify the left and right of the list. The index entered on the left side of the colon determines the first object. This value includes the object at this index. The index entered on the right side of the colon is the index to stop at but not to include.
For example: image we want to extract just the b and c. We could start at index one and go up to but not including index 3. It’s common to need to extract a slice of objects from a collection such as a list. This syntax helps us to do that by specifying the range of objects. Python tries to make accessing different elements in a list easier by allowing negative index values. For example, we can access the last object in a list using the index of: negative one. This tells the interpreter to go backwards by one object. We could obtain the second to last with negative two, and so on.
This means we can also do interesting things such as extracting all but the last object. When using the range syntax, the starting index defaults to zero. And the ending index defaults to the last index. Knowing this, you can omit either, if it’s only going to be using the default. For example, if you want to extract the last two values you could start at negative two and go to the end by leaving the end-index blank.
Don’t worry if all of this new syntax doesn’t quite stick. In reality it is more important to remember the capabilities rather than the exact syntax. The way to master syntax is through hands-on coding. Which will happen over time. Python includes several operators which are used to operate on sequence objects. Which means we can check to see if an object does or does not exist in a list. Using the in and not in operators we can produce a boolean object. Here’s an example.
We can determine if the letter c is in our list of letters. In this case, that’s true. If we were to check if z is in the list of letters, it would return False. We can also use the negative form of this which is not in. Since z is not in the list of letters, this returns True. Lists are one of Python’s more common object types. They’re used in some way in most applications. When working with lists it's common to need to know how many objects are in the list. The built-in len function is able to determine how many objects are in a list. By passing a list to the len function it will return an integer representing the number of objects.
Okay, this seems like a natural stopping point. Here are your key takeaways for this lesson:
- The syntax for creating lists is based on square brackets
- Each object in a list exists at a numeric index - which starts at zero
- You can access an individual object based on its index
- You can also access a range of objects using the range syntax.
- You can determine if an object does or does not exist inside a list using the in and not in operators.
- The built-in len function will return the length of a list.
Alright, that's all for this lesson. Thanks so much for watching. And I’ll see you in another lesson!
Ben Lambert is a software engineer and was previously the lead author for DevOps and Microsoft Azure training content at Cloud Academy. His courses and learning paths covered Cloud Ecosystem technologies such as DC/OS, configuration management tools, and containers. As a software engineer, Ben’s experience includes building highly available web and mobile apps. When he’s not building software, he’s hiking, camping, or creating video games.