 In this video we are going to learn about the list data type in detail. So let's start a new file and let's rename it into simply list.ipython notebook. So list we have seen many times before in this course, so now let's look at the details. So first of all you remember that to create a list we are going to use, or one way to create a list is to use the so called literal notation with the brackets and put the elements of the list in between the brackets. So if you want to create a list that is initially empty, the way to do that is to simply write open closing brackets and that will already create a list object that has no elements in there. And also as we have seen in the video who am I and how many is that list objects are mutable, that means creating an initially empty list is quite useful sometimes because you can still put in elements after you created it. So let's assign this empty list to a variable that we simply call empty. Okay so empty is simply the empty list. Okay let's do another example. So let's take let's call the list simple because it's going to be a simple list and we are going to see what what I mean by by simple and non-simple in a bit. So let's create a list that simply holds two numbers let's say 40 and 50. Okay so let's create the list and the simple list now has two numbers in it. So this is how the list syntax works and now let's do one more step and let's see how far we can go with this with this syntax. So what we could do is we could go ahead and we could create a new list and the list now has as its element first a reference to the empty list. Okay so what we learn from that is that I can use variables as an element when I create a list. So sometimes this is useful to know. So then let's go ahead. After we put in the empty list there let's put in there the number 10 as the second element as an integer. Next let's put in the list the number 20 as a floating point number here. Then let's go ahead and have another number. Let's put in there the number 30 but now modeled as a string object so as a text string and let's end the list by putting another reference to a list in there namely the simple list. So this is going to work as we see this is going to create a list object with the mentioned elements in the list. Now what is so special about this list? Well I'm using this example to illustrate some points. The main point that you should take away from this is that they don't contain the elements themselves they only contain references to the elements and in this example because we have a list that contains a list that contains elements we also say that the data here is nested and nested data is a term that we use to indicate that we have basically a collection inside a collection. So something inside something inside something maybe and you can do that a couple of times before you get to the final elements that you actually care about. We could use that nesting strategy to for example model a matrix. A matrix is simply a list of vectors if you want right it could be a list of row vectors or a list of column vectors it doesn't matter but at the end of the day a matrix is just a two-dimensional list in a way. But we will learn about the details quite in a bit. So now that I have given you some examples what I'm going to do is I'm going to draw you a memory diagram on how this looks like in memory. So first we created the empty list and the empty list is simply created in the following way we are going to create a list object and we have already learned that list objects are these long so-called arrays but the term array is in python used for something else and they have fields in it that have the same space in between. So in other words they can hold the same number of bits in each field and all these fields hold are references. However this first list is going to be empty so there are no references inside this list. So this list has a name we call it empty and so let's put the empty name here with the reference to the list. Then we went ahead and we created another list so let's simply do that just like this. We create another list here with a couple of empty fields and the exact number doesn't matter it's a so-called implementation detail but you can already guess that python already puts in a little bit more fields so that you have some space to work with. So let's say if you create an initially empty list and you want to later on put something in you don't have to create a new list object you can simply use it and use the fields that are already there. Now in this second list object we put in the numbers 40 and 50 and this is going to look like this. We first have the number 40 as an integer and also we have the number 50 as an integer and now comes into play what I just told you that lists are just containers holding references to other objects. So that is how we model that and now this list has a name we call it simple. So the simple list simply looks like that. Okay now comes the third example which is a list as well so let's put in space for the list same number of fields but again the exact number doesn't really matter so this is a list and now we learned that the first element was a reference to the empty list right so this is what we see in the example code right here the first reference is or the first element is a reference to the empty list so what should we do here well it's quite simple actually so all we do is we put in a reference that goes up here and simply points at the other list that's it so we are not holding any particular data here the only thing we are holding is a reference to another list that may hold data that may hold references to the actual elements the second element here was the number 10 so let's maybe put it right here number 10 as an integer and let's put a reference right here the reason why I put it here is just to indicate that Python chooses for us where to put all the elements in memory we have nothing to do with it so in other words this is also one of the reasons why Python is such an easy language because for beginner especially because we don't have to deal with where to put something in memory Python does that for us the second the third element sorry was the floating point number 20.0 so let's write 20.0 and let's make this box a little bit bigger than the others because it's a floating point number just to illustrate that we have different data here and let's simply put a reference right here okay so the next element we said is going to be a string so an object like this which says the words 30 and this is a string so this is going to be right here and now the fifth element is really a reference to the other list up here so we could do it like this maybe so this is what this looks like in memory and then we gave a name to this list down here and we called it nested to introduce the idea of a so-called nested data structure so now here in this diagram we can already see where the name nested comes from so we have a list but the list does not contain only data but it also contains references in particular this reference here to the simple list that then contains further references to the actual data objects at the numbers here and whenever we have a structure where we have a reference going to some object that itself holds references to other objects that itself holds reference to other objects that is what we mean by saying nested data okay there will be an exercise in the in the future later on in chapter 9 I guess where we work or where you as a student work with nested data structures but you will until then learn about another data type the so-called dictionary data type to make the exercise a bit nicer but so far this is already enough to model nested data here okay and there is no one kind of nested data nested simply refers to the idea that we have references following references following references and so on okay so one other thing to remember is and let's continue that here in the slides so another thing to remember is we can of course because lists are sequences as we saw in the previous video they fulfill all the five all the four properties that every sequence has so let's look at these properties so let's first look at the container property let's simply go ahead and ask is the number zero in the nested list and we get back false so this indicates that the in operator works so in other words lists are containers just to as a little review the second property is the length of a list so the sized property so we are going to call the length function and now I'm going to pass in a reference to the nested list and now we get back to number five right but now if we look at the representation of the list here under cell eight what we see is if we count the elements inside some people may be confused because we could count like this one two three four five and six okay so the last list the last inner list here contains two elements so some people may say okay this list should have a length of six right however the length is five now why is the length of the list five well the length is defined to be the number of references a list holds so here we have one two three four five references even though the last reference is a list with two references these two references do not count towards the length of this list here only the references that are right here in this list object count towards the length of this container so this is why we see the number five here this is the second sequence property the third one is quite simple we can loop over it so we could say for element in nested and just to illustrate another point we are not going to print out the element but we are going to print out the type of the element and we see that we have different data types that are stored in the same list and this may be something new for those people of you that have experienced with some other programming languages in many programming languages the list like data type only allows you to model homogenous data so all elements must have the same data type here we see that the python list data type allows us to model heterogeneous data so in other words every element can have can have the same data type but they can also have different data types and in this example we see that elements do have different data types okay so in python there is another data type called the array especially the numpy array the third-party package numpy that we have also seen briefly before that also provides the array data type that is very much like the list data type but that only models homogenous data so this also exists in the python world but lists are not there for homogenous data they are typically there to you know contain simply references to whatever we want however in real programs in real life programs it is often not really beneficial to have a list with heterogeneous data so because usually when when you model something in a program with a list then usually what you want to do is you want to process all the elements in the list and that means that all the elements in the list have to support the same operations and usually the best way to to ensure that is to simply have a list with object of the same data type because then you can do the same operation for every element in the list however we are not forced to do that so that is the third property of a sequence and the fourth one was the ordered property so let's go ahead and simply say reverse so that's the reverse built-in that's passed to it maybe the simple list now and we get back something that is called a list reverse iterator what an iterator is we will learn in chapter eight but for now the short version is an iterator is simply a rule that knows how to calculate the next element without having without having calculated it yet okay so it's like this is a so-called a postponed operation or something that will happen in the future maybe but it has not happened as of now so I'm using this here to show you another way of how to construct lists and the other way is to simply use the list constructor so if I pass the reverse iterator to the list constructor here I get back a list with the numbers 50 and 40 so it's the same element as above just in reverse order the list constructor can also be used for example with a string okay so let me go ahead and let's say maybe say lorem ipsum that should already be enough and if I execute that what I get back is a list where each element is a one character string okay so why is that well let's look at that so let's go into the python docs library reference built-in functions and now let's look at the at the description of the documentation for the list constructor here so let's go here and we see that the list constructor takes one parameter as its input and it's called iterable and now with this video and the previous video we know what that is it's any element or any object over which we can loop and the string object itself is can be iterate can be iterated over and we have seen also in many previous videos when we loop over a string we are going to loop over the characters one by one therefore the individual element over which we loop is the individual character this is why we get back a list with individual or with a single character strings okay this is maybe confusing to some of you but the reason is simply because the list constructor takes any iterable and makes a list where each element in the new list is an element drawn from the interval over which we can loop okay okay so these are the four list properties and now to finish this video is also by referring to what we saw with the string data type when we also talked about these four properties in in the relationship of the string data type now we have these four properties so what can we do with it well they ensure that we can do for example indexing so if you go ahead and I say I take the nested list and let's say I index into the list using the indexing operator which also happens to be the brackets operator and let's say I want to access the first element I can simply do so by using index zero and I get back a reference to the empty list okay and of course because of the second property because of the second property which is the size property we know that there must be an end so there must be a highest possible index and therefore what we can do is we can also go the other way because if we know to begin with that there has to be an end that means we can also go backwards from the end and indexing backwards we have seen before when we talked about strings we can do with the index negative one so negative one will be the first element going from right to left and index zero will be the first element going from left to right here okay so this is indexing maybe let's give it a nice header here and now let's also generalize that we saw how that works before also when we talked about the string data type and the generalization of indexing is simply slicing so slicing means we not only get one element out of a sequence but we get many elements so let's go ahead and get from the nested list here let's get the two elements 10 and 20 and maybe also 30 so how can we do that well the way to do that is by using the indexing operator and now instead of putting the one index in there we are going to put the first index or the index of the first element we want which is index one for the second element and then we put a colon there and then we are simply going to put in this case the number four why four well the right index is the first index that is not included the left index is always included we want three elements for the three numbers in the middle and by saying go from one to four that means we are going from one including to four excluding which is exactly three elements okay so we get back another list object with the elements 10 20.0 and 30 this is slicing and of course just what we saw in strings also is for slicing we can also use a third number which is the step size so let's say I want to go from the first element until let's say the last one the last one will be element number four so therefore I have to add one because the upper index is not included so let's go to index five and let's do so in steps of two okay so let's take only every other element just like that and I should not write nesting I should write nested and then we see I get the empty list the 20 and the list with the numbers 40 and 50 in it and now as we also have learned with the string data type whenever I do slicing and I start at the beginning instead of simply writing this index zero I can simply skip it I can simply leave out the zero if I slice until the end of something and what I can also do is I can simply skip the upper index just like that so what remains is colon colon two which basically means gives me every other element going from the first to the last okay so that's the repetition of slicing and if you want to reverse a list what you could do is you could simply go ahead and say colon colon negative one so go from the beginning to the end in step sizes of negative one and this will basically flip the order okay so this is similar to the reversed built-in okay so reverse is the more formal thing probably to do it and by using slicing for slicing the slicing with the step size negative one is kind of a special case we could also of course slice with any other number as we saw okay so one last thing in this video to remind you we saw that before in the video who am I and how many lists are mutable mutable mutability means we can change the elements after we created the list object so let's go ahead and let's simply go ahead and exchange maybe the number 10 so how could we do that we can go ahead and we can say let's take index one to get to the number one and if I want to exchange let's say into the number 99 I can simply do that by assigning the number 99 to the to the second index so index number one and if we look at the output below here again now we see that the number 10 has been replaced with the number 99 this is not doable for all objects so there are immutable data types in python as well we will also see immutable data types in this chapter 7 on sequential data but the list data type is a mutable data type therefore we can change elements okay after we created the object okay so this regards the list data type the basics of the list data type in the next video I'm going to use the same example the nested example and I will talk about the difference between deep and shallow copies which is also quite interesting and then we will continue by looking into things that list objects can do so I will see you in the next video