Python: Learning Lists

Aldrin Caalim
Dec 15, 2020 · 8 min read
Photo by Alicia Slough on Unsplash

This is a summary of Chapter 3 of “Python Crash Course: A Hands-On, Project Based Introduction to Programming” by Eric Matthes

What is a List?

A list is a collection of items placed in any order. Lists can be made with all valid data types such as letters, numbers, strings, etc. The items within the list do not need to have any relation, although most tend to create lists based on items that are related to one another. Because lists usually contain more than one item, it’s a good idea to make the name of your list plural, such as numbers, letters, characters, etc.

In Python, lists are identified by the use of square brackets “[]”, while individual items in the list are separated by commas. For example:

How to Access Items in a List

You can access any item in a list by writing the name of the list which will then be followed by the position, most notably known as an index, of the item while enclosing the index in square brackets. For example:

You can also manipulate the item by using methods. For example:

Index Positions

An important thing to note is that the first item in the list is index 0. This means that the second item in the list is at index 1. To better visualize it:

As you see in the above example, there are a total of 4 different items in the list, but the index starts the count at 0 up until 3. This trend would continue with the index being 1 less than the number of items in the list. You could also think of it as “index = (number of items in the list) — 1”.

You can also use negative numbers to represent the index. For example:

Using Individual Items from a List

You can use individual items in the list just like you would with a variable. For example:

As you can see, you can call certain items in the list by stating the name of the list you are referencing, and then targeting the specific index of the item you want. All while being able to concatenate it and store the entire value into a variable. Here is that same example, except this time it will target fish with its negative index:

Modify the Items in a List

Items in a list are mutable, can be changed. To change an item in a list, use the name of the list followed by the index of the item you are changing, and then provide the new value you want to provide the item. For example:

The first result is just regurgitating the original planets list. While the second list had the item in the second index change from “breadsticks” into “venus”.

Adding Items to a List

The easiest way to add a new item to a list is to use the .append() method. And when we use .append(), it places the new item at the end of the list. For example:

The .append() method allows us to build lists dynamically. For example:

This is a common way to create lists because you won’t always know the data your users want to store in a program until the program is running. By creating an empty list to hold the users’ values and then appending their values as they provide the information, you allow the users to be in control.

Inserting Items into a List

Use the .insert() method to add a new item into any index in your list. For example:

By using the .insert() method, the items in the array, starting from the stated index, all shift one position to the right.

Removing Items from a List

Two ways to remove items from a list: using the del statement as well as using the .pop() method. By using the del statement you will no longer have access to that value. For example:

The .pop() method allows us to still have access to the removed item from the list. The term pop comes from the idea of a list as a stack of items and popping one item off the top of the stack. Think of it as “last in, first out”, that is how .pop() works. The last item in the list will be the first removed when using the .pop() method. For example:

One useful way to use the .pop() method would be like this:

By default, the .pop() method will remove the last item in the list. But you can use the .pop() method to remove an item at any position by stating the index within the parenthesis. For example:

Keep in mind that using the .pop() method means the item will no longer be stored in the list. When you want to delete an item from a list and don’t plan on using that item anymore, use the del statement. If you want to use an item as you remove it, use the .pop() method.

Removing an Item by Value

If you don’t know the exact index of the value you want to remove from the list. You could always use the .remove() method if you know what you want to remove. For example:

As you see above, by stating the exact value you want to remove from the list, the program will remove the first instance of it. For example:

Here’s an example of how you would use the .remove() method:

Sorting a List Permanently with the .sort() Method

The .sort() method allows us to order lists in alphabetic or numerical order. For example:

If you decide to mix string data types with the other data types, you will receive an error if you use the .sort() method. For example:

But numbers and booleans will work in conjunction with one another.

You can also reverse the order by inserting the “reverse = True” argument into the .sort() method. For example:

Keep in mind, the .sort() method permanently changes the original order of the list.

Sorting a List Temporarily with the sorted() Function

The sorted() function allows you to display the list in a particular order without permanently altering the order of your original list. For example:

As you can see, by using the sorted() function instead of the .sort() method, we can access the original list before the order was manipulated. We can also use the “reverse = True” argument in the sorted() method. For example:

Sorting lists can be increasingly complex because of capitalization, but that is why we just use the .lower() method to maintain consistency between the items in our lists.

Printing a List in Reverse Order

The .reverse() method allows us to reverse the order of our original list. For example:

Note that the .reverse() method doesn’t sort the letters list in reverse sorted order. But that it reverses the order of the original list. The .reverse() method alters the order of the list permanently. But you can revert to the original list by just using the .reverse() method again. For example:

Finding the Length of a List

Use the len() function to find the length of a list. For example:

Avoiding Index Errors When Working with Lists

Remember, when you use that syntax, “numbers[5]”, you are calling the index. Python is a zero-based numbering system. In short, we start the index count of a list at zero.

If you do get an index error save yourself time by just printing out the length of the list using the len() function and then subtract that length by one.

Probably Programming

Probably about Programming

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store