1.7: Lists - nealtran1905/PythonForResearch GitHub Wiki
Lists are mutable sequences of objects of any type.
And they're typically used to store homogeneous items. Lists are one type of sequence, just like strings but they do have their differences. If we compare a string and a list, one difference
is that strings are sequences of individual characters,
whereas lists are sequences of any type of Python objects.
Another difference between strings and lists
is that strings are immutable, whereas lists are mutable.
In addition to these two differences, strings and lists, of course, come with their own methods. It is common practice for a list to hold objects of just one type, although this is not strictly a requirement. Let's try a couple of simple lists to experiment with them. Let's construct a simple list of numbers to learn a little bit more about lists.
So I'm going to construct a list of numbers.
I'm going to call it numbers. And I'll use numbers 2, 4, 6, and 8. Imagine I wanted to extract, or access, the first element of my list. The first thing for me to do is type the name of the list, then I need my square brackets. Now remember, in Python, indexes start at zero. So for me to be able to look at the first element of that list, I need to put in index 0, position 0. Here, Python tells me that the first object, meaning the object located at position 0, is number 2. If I change the index to 1, Python gives me the second object. Now if I wanted to find out what is the very last object on my list, I can count positions from right to left. And that means I have to use negative indices. So if I type numbers minus 1, Python tells me the very last element of my list is the number 8.
numbers = [2,4,6,8]
numbers[0] = 2
numbers[1] = 4
numbers[-1] = 8
Remember, because lists are mutable sequences, we can modify their content and after we've created them. Let's try appending a new number, a number 10, to the end of our list. To do that, the first thing I type is numbers, then I need my dot, and I type append. And inside the parentheses, I put in the number 10. If I now ask Python to show the content of the list, I'll see that number 10 has been appended to that list.
numbers.append(10)
numbers
[2,4,6,8,10]
Another operation we commonly would like to do is to concatenate two or more lists together. Now consider a situation where I have another list. Let's just call that x. And let's say the new list has numbers 12, 14, and 16. In Python, I can use the plus sign as long as both objects are lists to concatenate them. So I can type numbers plus x. And you see that the result is another list.
x = [12,14,16]
numbers + x
[2,4,6,8,10,12,14,16]
I can check that by asking, what is the type of the object that Python most recently outputted? And that's a list.
type(_)
list
So when I type a list plus another list, I'm concatenating these two lists together.
Let's now look at a couple of list methods in a little bit more detail.
In this situation, we have a numbers list. And what I would like to do is, I would like to reverse the content of the list. So I would like to have my last object first, second to last object second, and so on. In this case, I can use the reverse method.
numbers.reverse()
numbers
[16,14,12,10,8,6,4,2]
So I can type numbers, and I can call the reverse method. But see, Python doesn't return any object to me. This is because list methods are what are called in-place methods. They modify the original list. To see that, I can now type numbers, and I can see that the numbers on the list have been reversed. So this is what happens. If we have a list here - we have our numbers 1, 3, and so on. Our last number is 23. What the reverse method does is the following: It operates on the list that I have, meaning the original list. It moves the last object first and the first object last. Then it takes the second to last object, moves that to the second position, and so on. So the consequence is that the ordering of the objects in the list will have been reversed by the end. Let's then a look at the sort method. Consider a list called names where we have four names, Zofia, Alex, Morgan, and Anthony.
names = ["Zofia", "Alex", "Morgan", "Anthony"]
One of the list methods is called the sort method. If I type names.sort and place parentheses at the end of sort, I can sort the contents of this list. If I now type names, you'll see that names have been sorted to be in alphabetical order. So the new ordering of objects in the list is Alex, Anthony, Morgan, and Zofia.
names = ["Zofia", "Alex", "Morgan", "Anthony"]
names.sort()
names = ["Alex", "Anthony", "Morgan", "Zofia"]
Now there is another function in Python which is called "sorted". And the way that function operates is a little bit different. When we're using the list method sort, we're taking the existing list and reordering the objects within that list. If we're using the sorted function, we're actually asking Python to construct a completely new list. It will construct this new list using the objects in the previous list in such a way that the objects in the new list will appear in a sorted order. So let's see how this happens. Let me take my names list, and let me just reverse that. And to confirm what has happened, now the ordering has been flipped as we would expect. I can then say something like sorted_names, which would be a new object. I use the sorted function and I apply it to my list names. Now if I see what is the content of names, you see that nothing has happened. That's because I haven't called any list method. But if I look at the contents of sorted_names, you'll see that it has the same exact objects as the original list names, but in this case, they have been alphabetically sorted. Finally, if you wanted to find out how many objects our list contains, we can use a generic sequence function, len. So we can type len(names), and Python tells us
len(names)
that our list contains four objects.