 Let's consider this program to get the average of seven days worth of temperatures in degrees Celsius Let's run it and It tells us the average temperatures twenty eight point three degrees Nothing remarkable here except it's very repetitive. We need an individual variable for every one of the temperature measurements What if we had a whole month's worth of temperatures? That would be really difficult We'd have a lot of copying and pasting with a lot of room for error And can you imagine what line 12 would look like if we had to add 30 numbers up all together? Instead of having a separate variable for each day We can use a list to group all of the values together under one name If you think of an ordinary variable as a mailbox for an individual house You can think of a list as a row of mailboxes for an apartment building They all have a common street address with an apartment number to distinguish one from another in This case the case of a list our apartment numbers begin at zero instead of one and As we saw with strings we use square brackets to index into the list Here's a version of the temperature program that uses a list as you saw in chapter 4 You create a list by putting its elements in square brackets separated by commas And you access a list element by putting its index numbers in square brackets as you see on line 6 Now you may be thinking that's not much of an improvement. Yeah, we don't have the individual variables anymore But line 6 is still as long. In fact, it's longer because we've had to add the square brackets But remember just as you can go through a string one character at a time with a for loop You can do the same thing with lists So instead of this big long expression I'm going to set the sum to zero and then they say for each item in the day list Set the sum to sum plus the item That's a lot shorter. And if I run the program It still gives me the exact same result One advantage of lists is now if I have another week's worth of data Let's add some other numbers like 30 31 29 29 30 30 and 32. I Don't have to change this part of my program at all. It's exactly the same. It just goes through every single item in the list I do have to change this line because now I have 14 numbers instead of seven But to avoid even that retyping I'll replace the seven with the length of The day list then no matter how long or how short my day list is I'll always get the correct answer Let me go back to my original seven entries and you'll see it's working correctly And this is the beauty of using lists with for loops They work hand in glove with one another to make your life very easy when you have a lot of values That are all grouped together Let's look at some more list operations and we'll start by making a list of strings in This little program where I set the words list to be the Number zero through five spelled out Again, you access a list with square brackets zero is the first element and Five is the last element because there are six things in this list That's the length of words You can also use negative indices word sub negative one gives the last entry Excuse me, let me type that correctly words negative one gives me five words negative two gives me four If I go off the edge of the universe such as words sub six or words sub negative seven I'll get an index error because my index is out of range Surprisingly, there's no find function for list, but there is an index which returns the position if an item is found in the list I Can say words dot index of the word three and that gives its position If I say something that's not in the list It'll give me a value error I can avoid that sort of error by using the in and not in operators For example is two in words. That's true is 14 not in words That's also true. Let me show you this in operation in a program I'm going to say number equals input of Enter a number in word form and then I can say if the number is in words I'll print found at index Words dot index of number. I know I'll find it. I know I won't get an error because the number is in the list somewhere Otherwise if it's not in the word list, I don't want to generate an error met an error an execution error. I Want to have my own custom error saying sorry that is not in the list Let's run this and If I type in three found it index three Run it again and put in seven and That's not in the list We can take slices of arrays We can take words starting at index one up to but not including Four if I leave off the ending number. I get from the starting position to the end The slice starts at position index zero One really big difference between strings and lists are that lists are mutable. You can change elements of a list for example, I can say words Sub one equals Uno and now if I look at my words list it has Uno in position one. I Can change the slice to a slice of equal size Let's take the words from two up to But not including four and change them to the words dose and trace and those two have changed You can also change a slice to a different size I can say words from two up to but not including four is going to be a list consisting of a single item dose E trace two and three I can go back and change it again and add more items Let's take words from two to three, which means only item number two and Let's change it back to dose Trace and add some extra stuff And now I have extra stuff in the list starting at location Four while I could use slices to delete part of a list by replacing the slice with an empty list It's more readable to use delete DEL I can delete the items in the words list from four up to but not including six and I'm back to the where I had it without the extra stuff One more thing before we wrap up this video Most of the times arrays will have similar elements as in our examples all of the elements in the first program We're all numbers temperatures in this example all of the elements in the list or strings That's normally the case, but it is possible to have elements of different data types Such as this I can say person equals and the first element of the list will be a string The second will be an integer and the third will be a float to represent a Represent and this ever represents this person's name their age and their weight in kilograms It's also possible to have lists inside of lists and that's a topic we'll get to in another video