Lists
Last updated
Last updated
Everyone knows what a list is right? We have TODO lists, word lists, homework lists. They are everywhere. Well, they are in computers too. Lists help us organize values we might be interested in into a single collection. We can then perform operations on each element of the list and add or delete items. In Python we can create a list by doing the following:
We store lists in variables like we have been doing with integers and strings. The syntax has us encapsulate or surround the values with square brackets.
Try creating your own list with the following values: 1,
"2",
"three", [4]]
and then print out the list!
Some languages enforce that lists only hold values of the same type (e.g. integers only or strings only). Python doesn't care as much. Some people view this positively and some view this negatively. It is definitely nice to have all of the flexibility you want when coding but sometimes it can introduce sneaky bugs that are difficult to track down. Whether you like it or don't like it, you should be careful when doing things like I just had you do.
To get the values out of a list we have created we do the following:
This will give us the value of 2
. What!? Shouldn't it be 1
? Nope. The answer is 2
because in computers we almost always start at an index of 0
. The word I used here is index which refers to the action of finding an object. If you went to a library and wished to get a book, you would look up the book's index which would lead you to where it is on a shelf. The same concept applies to indexes (or indices) in a list. The index is the item's position in the list. We use those square brackets after the variable name to tell Python that we wish to index the list at the desired position.
Python allows us to do some cool things with lists. One of these is the ability to index a list from the back instead of the front. This is called a negative index and when performed on our list would give us the value of 3
.
We can also create sublists from existing lists. This is called slicing. It looks just like indexing but we supply two values instead of one.
We have used the len()
function with strings but it can also be used with lists to determine how many objects are in lists.
We have seen certain keywords already but certain keywords have different meanings when applied to lists. The in
keyword was used previously in a for
loop. When used with lists we can have Python tell us if a specific value exists within the list.
We can also use some of our familiar operators with lists. We can concatenate (combine) two lists together with the +
operator.
We can also combine things together multiple times using the *
operator.
There might be a time when you want to set multiple values from a list at the same time. Python allows you to do this with multiple assignment.
This is all really cool but how do you add things to a list when the program is running? Lists have functions that you can call on them. This means that when you have a list like: my_list = [1, 2, 3]
you can call functions on my_list
. We call these member functions because they are members of List.
Try sorting the list we created earlier [1,
"2",
"three", [4]]
!
You should have found an error when trying to sort. That is because Python doesn't know how to sort values of different types. This is the trouble I mentioned you can get into if you create lists with different types. Python gives you some flexibility to do what you want but sometimes that flexibility might make things challenging in the future.