Learn Python For Beginners

Lists In Python

Instead of one value, lists in python can hold many values. This is a great way to organize data that is needed for similar purposes. This is a continuation of the series on this website for learning python for beginners and it’s important that you know everything from the previous lessons. Let’s take a deeper look at lists.

Writing Lists In Python

Declaring a list in python is similar to declaring variables in python. Simply close your values inside of square brackets and separate them with commas. You can also declare an empty list and augment its values afterward.

Lists In Python

It may not seem like it now, but understanding lists is a powerful thing. Instead of just teaching you how to type code and moving on, let me try to elaborate with a hypothetical. Let’s say you decided to write a web application that uses python to perform requests for information about The Legend Of Zelda Characters over the internet using an API. It may be confusing to look at now but go to this page and try to follow along. On the left, click on the characters route endpoint in the menu.

After that, while the code snippets pill is selected, in the drop-down menu select python.

Then, click on example responses and click the arrow next to the data variable which contains a list of objects that contain the data we would want to use.

Since we know the response is going to give us the data we want in a list we could store that data in a variable and then reuse it when we need it. We will be doing something like this later but for now I just wanted to illustrate a common scenario where you might actually apply the knowledge in this section. Operating on lists in python is a broad subject so let’s look at some of the fundamentals before moving on.

Operating On Lists In Python

Each element in a list has what is called an index. This is the value’s position in the list left to right starting from 0 and continuing on. We can access a value in a list by passing its index into square brackets. We will write a program that prints Bobby’s name from our list of student names.

Lists In Python

Notice we used a variable to store the value before printing it. We use an index of 2 and get the third value in the list. This is because the indexes in order of the list would go 0, 1, 2. You can also change the value in the list using the index.

You can see that we changed the value of the third element in the list by using its index. It’s also possible to perform math with lists in python but it works different than normal.

Lists In Python

The operations don’t take the values in each list and add them together, they add one list to another to make a new list containing both previous lists. As a challenge before moving on, try to make a new list that would contain the sum of all of the numbers in each list. It would end up looking something like this.

It’s possible to store the sum of each value into a variable and then add each variable to an empty list as well but the method above is faster to write. Even so let’s go through the alternative approach just to see how the append method works on lists in python.

Lists In Python

The append method in python takes a value as a parameter and adds it onto the end of a list. This makes working with lists in python easy. It is also possible to run logic depending on the values in a list.

List Operators

There are also operators for lists we can use. A popular operator is the “in” operator which is used to check if a list contains a value. Using the knowledge from the python if statement section lets build a program that lets us know if a recipe contains dairy.

Lists In Python

There is also a not operator in python when using lists. The logic in our program can be extended to have a notification if an ingredient is missing.

Using python list operators offers more control of how data in a list . There are also functions that you can run that are helpful when working with lists.

List Functions

Earlier the . That was an example of a list function. Let’s look at some other functions we can use. There will be a lot of times where the length of a list must be known. You can achieve this by using pythons len function.

Having access to the length of a list can be helpful in many ways. For instance, in a use-case where the index of the middle element of a list with n elements is needed, dividing the length in half can find this information. Like the append method, using the insert method is a way to add values to a list. The insert method calls for an index where the new element will go as well as the value of the new element itself.

Lists In Python

The new value we added to the list was added at the index 2 instead of at the end of the list which would happen with append. Suppose there was a situation where an element was in a list but it was unknown where it was, the index method finds the first occurrence of a value in a list.

Lists In Python

There are a few more list functions for python that won’t be covered here but are easy to understand with common sense.

  • max(list) is a function that returns the element with the maximum value
  • min(lists) returns element with minimum value
  • list.cont(element) amount of times an element appears in a list
  • list.remove(element) removes element from list
  • list.reverse() reverses the order of a list

Conclusion

That was the longest lesson so far. Lists in python are going to be something you see quite a bit when writing your own software or looking at tutorials or open source projects. Understanding how lists work in python will prove to be a great skill. It is a skill that might take a little practice to master but is a necessity for writing efficient software. Don’t stop now though, because we’re moving on to loops in python.

Leave a Comment

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