Codecademy - Python - Tutorial #16

Creating an Empty List and Looping Through It

=====================================================

We start by creating an empty list, which is initialized to an empty list. This variable will hold our result. We then loop through each element in this list. The first time we do this, we're working with a list that contains lists within it.

Walking Through the First Element

-------------------------------

Let's walk through what happens when we loop through this inner list. The first element is 1, 2, and 3. We append these values to our result list. So, after this first iteration of the outer loop, our result list looks like this: [1, 2, 3]. Now, let's move on to the next iteration.

Walking Through the Second Element

---------------------------------

The second element in the inner list is also a list that contains numbers from 1 to 3. Again, we append these values to our result list. This time, however, our result list already contains values from the previous iteration, so the new values are added after those existing ones. Our result list now looks like this: [1, 2, 3, 4].

How the Outer Loop Ends

-------------------------

Our inner loop ends after we've processed each element in the inner list. However, our outer loop hasn't ended yet. We still have another iteration to complete. The next time we loop through the inner list, we're working with a new set of numbers: 5, 6, and 7. We append these values to our result list.

How the Outer Loop Ends

-------------------------

After completing the second iteration of the outer loop, we've processed all elements in both lists. Our function returns the final value of the variable `results`, which is a list containing numbers from 1 to 9.

A More Efficient Approach

---------------------------

There's an even more efficient way to accomplish this task without using inner loops. Instead of accessing each element individually, you could use the index of the current element and add it to the next one in the sequence. However, this approach requires access to the indices of the elements in the list, which can be tricky.

For example, if we're working with a list like [1, 2, 3], we could calculate the sum of consecutive numbers by using an index-based approach: `results.append(element at current index + element at next index)`. This would give us a new list containing only numbers from 4 to 9. However, this requires understanding how indices work in lists and can be more complicated than simply iterating through each element.

Tips for Looping Through Lists

------------------------------

When working with nested lists or sequences of any kind, it's essential to understand the indexing system used by these data structures. Accessing elements using their index is a powerful tool, but it also requires caution to avoid errors.

For example, if you're working with a list like [1, 2, [3, 4]], accessing `element at current index` might not give you what you expect. You need to keep track of the current position in the list and how many nested lists are still ahead of you.

To avoid confusion, always make sure you understand what's going on when working with nested data structures or sequences.

Conclusion

----------

Creating an empty list and looping through it is a fundamental concept in programming that helps build strong skills for more complex tasks. By mastering this technique, you'll be better equipped to handle a wide range of scenarios involving loops and data manipulation.

"WEBVTTKind: captionsLanguage: enhey guys what is up this is kazzy from cleverprogrammer.com and you are watching codecademy python part 16. yeah yeah i think it's 16 for sure okay so let's get started yeah this is getting complicated guys i i tell myself i have to remember it's part 15 or part 16 and then i forget so we're getting far along that's a good thing and you're getting far along that's even better i'm excited for you if you've gotten to part 15. okay so um don't tap yourself on the back yet do it maybe at the end of this tutorial hopefully we'll finish all of this and get started on battleships so they want us to do a lot of things here so they showed us two methods to print out items from a list which is exactly what i showed you guys in the last video remember so that's what they're effectively doing here um either just make that access that element directly right that definition of item came up of distinct physical object so either access that item in a list directly or access it using its index and then you can call the list and call and pass in the index when you're indexing the list and then print get access to each element that way method one is useful to loop through the list but it's not possible to modify the list this way so there yeah so that's true you can actually modify the list this way if you want to modify the list you have to know the index of that element and then you can change that index okay for example if i get access to this element directly let's say i'm looping through for item in n print uh or n i don't know equal or sorry yeah let's say item equals item plus one and then um i don't know i print out n three is still three even though i told three to increment itself by one but what actually happened is that this list got passed and we got a three just by itself and then it went okay the new definition of item is equal to what item was three plus one so now item is four and that variable item is just a variable by itself it's not really connected to the list in any way so what you want to say here is for i in range len of n this translates to 0 1 and 2. i've already shown you how that works but just to be just to kind of reiterate len of n this is 357 well length of 357 is simply 3. range of 3 gives you a list that's like this with 3 elements in it okay and then i will say n of i equals n of or i'm sorry i um plus one now if i print n you'll see this will be four this will be six and this will be eight just like here okay make sure you really understand that concept okay one loop you go through in this indices and the other loop you go through the actual element itself all right so now they're telling us a lot of things to do create a function that returns a sum of a list of numbers okay so it sums these guys and returns it so define define a function called total that accepts one argument called numbers it will be a list so we are expecting numbers to be a list inside the function create a result create a variable called result and set it to zero using one of the two methods above iterate through the numbers list so i'm going to say 4 i in range 0 through and i'm just writing 0 just to be very verbose as to what i'm doing it's not necessary at all if i want to go one step further i can even say start is equal to zero and stop is equal to that but um i'm not gonna go that crazy normal way would just be to pass in the length but i'm gonna just specify that hey i'm starting from zero and i'm going to pass in len of numbers which is whatever the list you pass that function and we're going to say that for each number we're going to as it added to results so i'm going to say result plus equals or result is equal to result plus that list indexed by that index that we loop through so you know this will be like first i will be zeroth uh element right i will be the zeroth element and numbers of zero will give you three then i will be the second element in the list that that will be 1 and numbers of 1 might give you back 5. we're assuming that we passed in this list 2 numbers and then it will be 2 so it will say at indic2 give me what numbers is and we'll get back 7. so on and so forth i'll go through this algorithm really quickly with you guys after we're finished writing it and then at the end return result because i really want to make sure that there's not a single part that we are skipping over then create a function called create a function called total that adds up all the elements of an arbitrary list and returns that count using the existing code as a hint use a for loop so it can be used for any size list so this will work let's take it to the ripple and do print our function total let's pass in the list n and let's run it let's see what it does 3 plus 5 plus 7 is indeed 15 and that's what we got right over here okay how did it work well let's see n is three five seven passing three five seven to this guy a length of three five seven evaluated to three range from 0 to 3 evaluated to 0 comma 1 comma 2 the first time in the loop i is 0 numbers of 0 numbers of 0 evaluates to uh numbers evaluates to 3 comma five comma seven uh this list indexed by the for the by the zeroth element gives you back three result is zero zero plus three gives you 3 and now result is 3. then the loop happens again and now this time i is actually 1 numbers of 1 gives you back a five here result which was three here it's going to be three plus five three plus five is going to give you back eight and now you're gonna have eight here it's gonna keep doing that until at the end result will give you back 15 and then since that function returns 15 this whole thing gets evaluated to 15 and it prints it out to the screen which is how you get to see it and screen is i think right above me so it gets printed out to the screen right there okay all right another way to do this is just to get the element uh directly here it makes more sense to get it directly so i would actually say for number in numbers uh result is equal to result plus number i think this is a much easier way to write this and it's better for this one because we don't care about the indices but let's go back to that because i think they're gonna make us use it using strings and lists and functions now let's try working with strings so you can do the same thing and do that with strings as well but you can't change values of strings because it's they're immutable we'll find out what that means create a function that concatenates strings so we're going to create a function that's going to keep looping through and adding the strings together that shouldn't be that hard if you guys really think about it you can do it yourself so take a stab at it first and then watch me do it so i'm going to say define string adder uh takes in list of strings and what it will do is for uh each string in the list of strings it's going to uh final string is going to be i'm going to start it off as an empty string so what it's going to do is every time it gets a string i'm going to say that increment the final string um by the final string plus the new string that we add okay and then at the end i'm going to say return the final string so let's create um grocery list let's say or let's just say groceries is equal to banana apple avocado maybe i misspelled that and berries okay and then let's say four or we don't have to actually write anything we've already wrote it let's call our string adder function on groceries and let's make sure to print that and let's run it let's see what happens and you get a banana then it adds apple to it and then it does all that okay if i wanna do it in a little bit of a better way i can say add an additional space at the end of each one so you'll get that space here but you also get that space right here which you don't really need but it's still cool you get these spaces here okay how does this work well let's go through it really quickly so groceries is just that list this gets passed into our function final string is just an empty string in the start and then what we do is that for each string in that so the first time string is banana and final string is an empty string and so it adds banana to that empty string now this empty string is banana um right banana this whole thing actually translates to banana with a space at the end so our final string is actually that then what happens is as we get to the second element apple actually we gotta yeah it's gonna get tricky to do but let's go let's just go back okay oops let's go back to the part where i had the list there cool so the second element in the string is gonna be an apple we have banana here so i'm just gonna leave that and then string is going to be apple this part was banana with a space and then we add empty space here so this effectively turns into banana space apple space and end and so then this guy is now that okay and this that's the way you get banana and apple and then it adds avocados and berries and it adds a space at the end cool okay so we created a pretty nice function and that's effectively what they want us to do so what i'm going to say is i'm probably just going to copy this guy and oops just copy this guy and maybe paste it here the function is called join strings so we're going to say join strings and what's the argument they want accepts an argument called words everywhere i have lists of strings i'm gonna call it words and for now this would make more sense to call it a word here word um no actually yeah word inside the function create a variable called result and set it to an empty string iterate through the words list that's exactly what they're saying right create a thing called the result and make it that instead of final string call it result right okay and iterate through the words list and append each word to result and finally return the result don't add spaces between the joined strings so we're not adding any spaces here right otherwise i would do something like this but i'm not cool okay and it works so you see michael and then it says lieberman yeah okay a little foreshadowing i'm gonna show you guys a really cool and advanced trick to do this a cool way to join all the things in the list is actually to do um this thing called dot join and then pass in that list so what this does is um it joins all the items in the list by this thing here so you can have this thing here be whatever you want uh i'm gonna have it be a space i'm going to say print so you're going to see what i mean in just a second here so you see it actually joined all the elements right each element banana apple avocados berries by this empty space here because i said give it to me by empty space let's say i told it to join it by this it would do just that so this is kind of a nice thing to uh to think about because if you're like creating a game like rock paper scissors maybe it's nice to have each thing separated and decorated in a way so when a person is somebody's playing your game it looks nice it looks formatted even if you're making just a text based game not you know fancy graphics you can still use these things to make it look nice you know you can have rock with some spaces in between and then scissors so it looks nice and separated just just some things to keep in mind but this is a little advanced and cool trick to know about okay lit using lists of lists and functions cool let's get to that using two lists as two arguments in a function so what does that even mean well it's really quite simple okay i'm going to define some function called join list and i'm going to say m comma n and i'm going to do return m plus n let me show you guys something cool skip so i'm just going to open up my idle here which is the same thing as having my rupple open but again i just like to open up multiple things so you guys know that you can do things multiple ways plus i'm just showing you in python two so in python two or three is actually the same thing if you have two lists you can just add them together plus four five six it just uh concatenates the list together that's it okay pretty simple right so if you wanted to join lists just use the plus operator it knows what to do it adds them together and then just return it so it's literally two lines of code oh wow they actually did the same example as us i wasn't even thinking about that that's hilarious i didn't even read all of this that's really cool so let's run it and you will see that that's what you got okay you can call these variables here whatever you want remember just as long as you are consistent i should still give you back the right answer i just called it mnn i'll call pq a b whatever you want really so let's take this code and put it in here and let's just call it um pq and the only reason i'm calling it different than this is so you don't get confused and think oh these two things have to be the same as the parameters no they don't it's q okay very easy start next lesson using a list of lists in a function so now they're having you do two loops uh what does this mean all right i'll show you just a second here okay four list in list of lists print list so what they're doing is let's say i gave you this list that contains how many elements take a guess it contains only two elements okay the first element in this list is this list here this is the first element and the second element in the list is this list here okay so that comma is telling you since there's only one comma separating these two items it's telling you that they're only two items okay so what does that mean well we're going to loop through what we want to do is loop through this and print out all the items in the first list and then continue looping and then print out all the items in the second list how the heck would we do that well first of all let's just write for lists and lists of lists right this is a variable list of lists lists of lists let's run it let's see what it prints out so effectively what it did is it passed uh you know that is that the first uh element is going to be one comma two comma three so this list comes one comma two comma three which is why up there you see one comma two comma three being printed out the first time and the second time the second element is four comma five comma six and so it prints out four comma five comma six which is why you see that as well right up there four comma five comma six okay but we want to print out one two three line by line and then four five six how in the world do we do that well let's try this let's say uh well how we would normally do that if we were just given one list so let's say for item in one comma two comma three we would simply do print item and that would give us one comma two comma three but here we have a list of lists so the first time it's a little complicated but it's actually quite simple because you're getting back a list so you can still loop through that right so we're imagining that we get back let's say one comma two comma 3. well you can do just that loop so you can say for item in that list print item so what will the loop do here's what it will do it will list of lists as that so the first time list is going to be 1 comma 2 comma 3 right 1 comma 2 comma 3 for item and list so for item in that list is going to be the first item is going to be 1 or i'm sorry 1 like that and it's going to print out 1 to the screen then it's not going to go back to line three it's gonna actually just keep traveling between line four and five till the loop here has ended okay it's gonna keep going like that then it's gonna go back to the outer loop on line three so next time the element is going to be 2 is going to come down here and uh you know since this variable is actually item this is item the second time in the loop this is going to be 2 and it's going to print out 2 which is you're gonna get this then it's gonna be again item item so both of these are gonna be three the last time and it's gonna print out three then it's gonna see that this loop is over because this is finished so it's gonna go back up here okay the outer loop is not over because it only became the first element it now needs to become the second element it needs to run through the second element so now it's gonna be four four five six right since this variable is lst or a list this is this this is going to be the same thing first so now we come to the second for loop here on line four item is gonna be four the first time and so we're going to print out four then item is going to be five we're gonna print out five and then item is gonna be six we're gonna print out six loop is over right so this kind of thing goes away um and then the we come to the outer loop and python realizes like oh we are done running through that outer list and we're finished and the program is going to be over at that point at that point okay so essentially what we did just to recap is looped through the outer list with the outer for loop and then once we got the inner list which was the first element then we looped through the inner list till the inner list was completed then we went back to the outer list and checked hey are there any more lists in there to loop through when we found another list we went to the inner loop and kept looping through that inner list okay so enough of the theory let's check if it works let's run it and you get back one two three four five six okay so it's called flatten so here's what they want us to do create a function called flatten that takes a single list um so and concatenates all the sub lists that are part of it into a single list so what we want to do is since we see one two three four five six seven eight nine all of that we wanna be able to turn it into um one two three four five six seven eight and nine into one list okay even if we have a hundred lists we wanna just turn all of that into one list so flatten one argument called lists and this is going to be pretty simple but let's let's go through what they're saying here make a new list called results so this is going to be an empty list and what we're going to do is we're going to iterate through lists so i'm going to say for numbers in lists that's what it says right iterate through lists call the looping variable number so the looping variable can be anything i'm going to call numbers here is going to be list so for numbers and lists now i'm going to iterate through numbers four number in numbers for each number appended to results results dot append number and then ultimately i'm going to say return results cool i think there's an easier way to do this as well which i'll show you guys after yes we earned the badge lists and functions we've completed it congratulations we've completed this course epic so now you can pat yourselves on the back i know you've been like holding it in right because at the start of the video i was like don't pat yourselves on the back so go ahead and pat yourselves on the back relax you know stretch whatever you have to do take a break walk around cool off your brain a little bit and then come back because we're not yet done so we're gonna go through it okay how did this how the heck did this thing work this is the crazy thing about them right they show you all of this stuff and then they're not going to explain how each step works because you could have just easily coded this by following their steps without even thinking how to do it all right so here's what's happening we're creating an empty list we loop through each element in that list so let's say the first element is actually a list so it would be better to call this variable list in lists rather than numbers because that's silly so the first time we loop through uh n which is these lists right over here right this is a list containing lists so the first time we loop through it okay let's just let's just walk it walk through it okay i'm gonna we call n here all right that's n we pass that into our uh lists variable and our argument now we have pass in list of lists again this function took only one argument keep that in mind this whole thing is just one list uh containing multiple lists inside of it result boom we initialized it to an empty list so what we did is we did that okay list is not that thing so for list in that the first element is a list one two three and list is one two three here as well number the first time we loop through is one and one we append one right here then the second time number is two it's two we append two right here then it's three right this three right here three three we append three loop ends what do we do we go to the outer loop we change these variables back to what they were number list number lists or sorry that's list this is list okay now our inner loop ended but our outer loop did not end because it only went through the first element we still have one more element left the second element in that is this guy here so the first number in that list is four four we append we go up here we append four then the second element in that guy is five we go and we append five then the next one is six we append six um seven results out of pen seven eight right results dot append eight and then it's nine and results data pen nine and it says return results so this function actually gets evaluated to that and then we call the print function on that list and uh you see that output on the screen okay so if i ran all of this make sure to put uh parentheses there and if i ran all of this you see a list one two three four five six seven eight nine that's how it actually worked now why i said there's an easier way to do this is what i would do is very simple i would say instead of doing an inner loop i would just say results that append um this gets a little bit complicated but so i'm not going to do it but it would essentially be uh element at index 0 plus the element at index 1. um so element at current index basically plus element at uh next index so what that means is in this giant list here it would do current index so let's say our index was at zero because this is this list is at position zero and this list um this element here is at position one so what it would essentially do is that plus that and this would automatically give you one list containing that okay and and it would just add it okay and then at the end you could do results of one or results of zero and get the final result but that just actually that complicates it a little bit um however um that would save you kind of going through another inner loop but you would have to do a different kind of loop for what i was saying because you need access to the indices so you actually need to do a loop with the range function okay so hopefully you understand how that worked let's take a look at our course page because we're done with lists and functions we're at 52 percent slowly but surely we're getting there and we're going to be at a really fun part in the next video we're going to be going through battleship and how to create an actual game how great is that then we have unit 8 left unit 9 is going to be awesome unit 10 i was thinking about skipping it but i'm gonna do it last because it's not useful so we're gonna actually get to more fun parts first that's what that means for you other than that that's it for this video guys you're watching um codecademy part 16 right this is kazi from clevverprogrammer.com please subscribe please like the video if it really helped you share it with whoever you want or maybe don't share it maybe that's your secret way of getting better at programming whatever's the case i hope you're enjoying yourself more than anything else and then i hope you're learning as well have a good rest of the week and i will see you in the next video codecademy part 17 okay take carehey guys what is up this is kazzy from cleverprogrammer.com and you are watching codecademy python part 16. yeah yeah i think it's 16 for sure okay so let's get started yeah this is getting complicated guys i i tell myself i have to remember it's part 15 or part 16 and then i forget so we're getting far along that's a good thing and you're getting far along that's even better i'm excited for you if you've gotten to part 15. okay so um don't tap yourself on the back yet do it maybe at the end of this tutorial hopefully we'll finish all of this and get started on battleships so they want us to do a lot of things here so they showed us two methods to print out items from a list which is exactly what i showed you guys in the last video remember so that's what they're effectively doing here um either just make that access that element directly right that definition of item came up of distinct physical object so either access that item in a list directly or access it using its index and then you can call the list and call and pass in the index when you're indexing the list and then print get access to each element that way method one is useful to loop through the list but it's not possible to modify the list this way so there yeah so that's true you can actually modify the list this way if you want to modify the list you have to know the index of that element and then you can change that index okay for example if i get access to this element directly let's say i'm looping through for item in n print uh or n i don't know equal or sorry yeah let's say item equals item plus one and then um i don't know i print out n three is still three even though i told three to increment itself by one but what actually happened is that this list got passed and we got a three just by itself and then it went okay the new definition of item is equal to what item was three plus one so now item is four and that variable item is just a variable by itself it's not really connected to the list in any way so what you want to say here is for i in range len of n this translates to 0 1 and 2. i've already shown you how that works but just to be just to kind of reiterate len of n this is 357 well length of 357 is simply 3. range of 3 gives you a list that's like this with 3 elements in it okay and then i will say n of i equals n of or i'm sorry i um plus one now if i print n you'll see this will be four this will be six and this will be eight just like here okay make sure you really understand that concept okay one loop you go through in this indices and the other loop you go through the actual element itself all right so now they're telling us a lot of things to do create a function that returns a sum of a list of numbers okay so it sums these guys and returns it so define define a function called total that accepts one argument called numbers it will be a list so we are expecting numbers to be a list inside the function create a result create a variable called result and set it to zero using one of the two methods above iterate through the numbers list so i'm going to say 4 i in range 0 through and i'm just writing 0 just to be very verbose as to what i'm doing it's not necessary at all if i want to go one step further i can even say start is equal to zero and stop is equal to that but um i'm not gonna go that crazy normal way would just be to pass in the length but i'm gonna just specify that hey i'm starting from zero and i'm going to pass in len of numbers which is whatever the list you pass that function and we're going to say that for each number we're going to as it added to results so i'm going to say result plus equals or result is equal to result plus that list indexed by that index that we loop through so you know this will be like first i will be zeroth uh element right i will be the zeroth element and numbers of zero will give you three then i will be the second element in the list that that will be 1 and numbers of 1 might give you back 5. we're assuming that we passed in this list 2 numbers and then it will be 2 so it will say at indic2 give me what numbers is and we'll get back 7. so on and so forth i'll go through this algorithm really quickly with you guys after we're finished writing it and then at the end return result because i really want to make sure that there's not a single part that we are skipping over then create a function called create a function called total that adds up all the elements of an arbitrary list and returns that count using the existing code as a hint use a for loop so it can be used for any size list so this will work let's take it to the ripple and do print our function total let's pass in the list n and let's run it let's see what it does 3 plus 5 plus 7 is indeed 15 and that's what we got right over here okay how did it work well let's see n is three five seven passing three five seven to this guy a length of three five seven evaluated to three range from 0 to 3 evaluated to 0 comma 1 comma 2 the first time in the loop i is 0 numbers of 0 numbers of 0 evaluates to uh numbers evaluates to 3 comma five comma seven uh this list indexed by the for the by the zeroth element gives you back three result is zero zero plus three gives you 3 and now result is 3. then the loop happens again and now this time i is actually 1 numbers of 1 gives you back a five here result which was three here it's going to be three plus five three plus five is going to give you back eight and now you're gonna have eight here it's gonna keep doing that until at the end result will give you back 15 and then since that function returns 15 this whole thing gets evaluated to 15 and it prints it out to the screen which is how you get to see it and screen is i think right above me so it gets printed out to the screen right there okay all right another way to do this is just to get the element uh directly here it makes more sense to get it directly so i would actually say for number in numbers uh result is equal to result plus number i think this is a much easier way to write this and it's better for this one because we don't care about the indices but let's go back to that because i think they're gonna make us use it using strings and lists and functions now let's try working with strings so you can do the same thing and do that with strings as well but you can't change values of strings because it's they're immutable we'll find out what that means create a function that concatenates strings so we're going to create a function that's going to keep looping through and adding the strings together that shouldn't be that hard if you guys really think about it you can do it yourself so take a stab at it first and then watch me do it so i'm going to say define string adder uh takes in list of strings and what it will do is for uh each string in the list of strings it's going to uh final string is going to be i'm going to start it off as an empty string so what it's going to do is every time it gets a string i'm going to say that increment the final string um by the final string plus the new string that we add okay and then at the end i'm going to say return the final string so let's create um grocery list let's say or let's just say groceries is equal to banana apple avocado maybe i misspelled that and berries okay and then let's say four or we don't have to actually write anything we've already wrote it let's call our string adder function on groceries and let's make sure to print that and let's run it let's see what happens and you get a banana then it adds apple to it and then it does all that okay if i wanna do it in a little bit of a better way i can say add an additional space at the end of each one so you'll get that space here but you also get that space right here which you don't really need but it's still cool you get these spaces here okay how does this work well let's go through it really quickly so groceries is just that list this gets passed into our function final string is just an empty string in the start and then what we do is that for each string in that so the first time string is banana and final string is an empty string and so it adds banana to that empty string now this empty string is banana um right banana this whole thing actually translates to banana with a space at the end so our final string is actually that then what happens is as we get to the second element apple actually we gotta yeah it's gonna get tricky to do but let's go let's just go back okay oops let's go back to the part where i had the list there cool so the second element in the string is gonna be an apple we have banana here so i'm just gonna leave that and then string is going to be apple this part was banana with a space and then we add empty space here so this effectively turns into banana space apple space and end and so then this guy is now that okay and this that's the way you get banana and apple and then it adds avocados and berries and it adds a space at the end cool okay so we created a pretty nice function and that's effectively what they want us to do so what i'm going to say is i'm probably just going to copy this guy and oops just copy this guy and maybe paste it here the function is called join strings so we're going to say join strings and what's the argument they want accepts an argument called words everywhere i have lists of strings i'm gonna call it words and for now this would make more sense to call it a word here word um no actually yeah word inside the function create a variable called result and set it to an empty string iterate through the words list that's exactly what they're saying right create a thing called the result and make it that instead of final string call it result right okay and iterate through the words list and append each word to result and finally return the result don't add spaces between the joined strings so we're not adding any spaces here right otherwise i would do something like this but i'm not cool okay and it works so you see michael and then it says lieberman yeah okay a little foreshadowing i'm gonna show you guys a really cool and advanced trick to do this a cool way to join all the things in the list is actually to do um this thing called dot join and then pass in that list so what this does is um it joins all the items in the list by this thing here so you can have this thing here be whatever you want uh i'm gonna have it be a space i'm going to say print so you're going to see what i mean in just a second here so you see it actually joined all the elements right each element banana apple avocados berries by this empty space here because i said give it to me by empty space let's say i told it to join it by this it would do just that so this is kind of a nice thing to uh to think about because if you're like creating a game like rock paper scissors maybe it's nice to have each thing separated and decorated in a way so when a person is somebody's playing your game it looks nice it looks formatted even if you're making just a text based game not you know fancy graphics you can still use these things to make it look nice you know you can have rock with some spaces in between and then scissors so it looks nice and separated just just some things to keep in mind but this is a little advanced and cool trick to know about okay lit using lists of lists and functions cool let's get to that using two lists as two arguments in a function so what does that even mean well it's really quite simple okay i'm going to define some function called join list and i'm going to say m comma n and i'm going to do return m plus n let me show you guys something cool skip so i'm just going to open up my idle here which is the same thing as having my rupple open but again i just like to open up multiple things so you guys know that you can do things multiple ways plus i'm just showing you in python two so in python two or three is actually the same thing if you have two lists you can just add them together plus four five six it just uh concatenates the list together that's it okay pretty simple right so if you wanted to join lists just use the plus operator it knows what to do it adds them together and then just return it so it's literally two lines of code oh wow they actually did the same example as us i wasn't even thinking about that that's hilarious i didn't even read all of this that's really cool so let's run it and you will see that that's what you got okay you can call these variables here whatever you want remember just as long as you are consistent i should still give you back the right answer i just called it mnn i'll call pq a b whatever you want really so let's take this code and put it in here and let's just call it um pq and the only reason i'm calling it different than this is so you don't get confused and think oh these two things have to be the same as the parameters no they don't it's q okay very easy start next lesson using a list of lists in a function so now they're having you do two loops uh what does this mean all right i'll show you just a second here okay four list in list of lists print list so what they're doing is let's say i gave you this list that contains how many elements take a guess it contains only two elements okay the first element in this list is this list here this is the first element and the second element in the list is this list here okay so that comma is telling you since there's only one comma separating these two items it's telling you that they're only two items okay so what does that mean well we're going to loop through what we want to do is loop through this and print out all the items in the first list and then continue looping and then print out all the items in the second list how the heck would we do that well first of all let's just write for lists and lists of lists right this is a variable list of lists lists of lists let's run it let's see what it prints out so effectively what it did is it passed uh you know that is that the first uh element is going to be one comma two comma three so this list comes one comma two comma three which is why up there you see one comma two comma three being printed out the first time and the second time the second element is four comma five comma six and so it prints out four comma five comma six which is why you see that as well right up there four comma five comma six okay but we want to print out one two three line by line and then four five six how in the world do we do that well let's try this let's say uh well how we would normally do that if we were just given one list so let's say for item in one comma two comma three we would simply do print item and that would give us one comma two comma three but here we have a list of lists so the first time it's a little complicated but it's actually quite simple because you're getting back a list so you can still loop through that right so we're imagining that we get back let's say one comma two comma 3. well you can do just that loop so you can say for item in that list print item so what will the loop do here's what it will do it will list of lists as that so the first time list is going to be 1 comma 2 comma 3 right 1 comma 2 comma 3 for item and list so for item in that list is going to be the first item is going to be 1 or i'm sorry 1 like that and it's going to print out 1 to the screen then it's not going to go back to line three it's gonna actually just keep traveling between line four and five till the loop here has ended okay it's gonna keep going like that then it's gonna go back to the outer loop on line three so next time the element is going to be 2 is going to come down here and uh you know since this variable is actually item this is item the second time in the loop this is going to be 2 and it's going to print out 2 which is you're gonna get this then it's gonna be again item item so both of these are gonna be three the last time and it's gonna print out three then it's gonna see that this loop is over because this is finished so it's gonna go back up here okay the outer loop is not over because it only became the first element it now needs to become the second element it needs to run through the second element so now it's gonna be four four five six right since this variable is lst or a list this is this this is going to be the same thing first so now we come to the second for loop here on line four item is gonna be four the first time and so we're going to print out four then item is going to be five we're gonna print out five and then item is gonna be six we're gonna print out six loop is over right so this kind of thing goes away um and then the we come to the outer loop and python realizes like oh we are done running through that outer list and we're finished and the program is going to be over at that point at that point okay so essentially what we did just to recap is looped through the outer list with the outer for loop and then once we got the inner list which was the first element then we looped through the inner list till the inner list was completed then we went back to the outer list and checked hey are there any more lists in there to loop through when we found another list we went to the inner loop and kept looping through that inner list okay so enough of the theory let's check if it works let's run it and you get back one two three four five six okay so it's called flatten so here's what they want us to do create a function called flatten that takes a single list um so and concatenates all the sub lists that are part of it into a single list so what we want to do is since we see one two three four five six seven eight nine all of that we wanna be able to turn it into um one two three four five six seven eight and nine into one list okay even if we have a hundred lists we wanna just turn all of that into one list so flatten one argument called lists and this is going to be pretty simple but let's let's go through what they're saying here make a new list called results so this is going to be an empty list and what we're going to do is we're going to iterate through lists so i'm going to say for numbers in lists that's what it says right iterate through lists call the looping variable number so the looping variable can be anything i'm going to call numbers here is going to be list so for numbers and lists now i'm going to iterate through numbers four number in numbers for each number appended to results results dot append number and then ultimately i'm going to say return results cool i think there's an easier way to do this as well which i'll show you guys after yes we earned the badge lists and functions we've completed it congratulations we've completed this course epic so now you can pat yourselves on the back i know you've been like holding it in right because at the start of the video i was like don't pat yourselves on the back so go ahead and pat yourselves on the back relax you know stretch whatever you have to do take a break walk around cool off your brain a little bit and then come back because we're not yet done so we're gonna go through it okay how did this how the heck did this thing work this is the crazy thing about them right they show you all of this stuff and then they're not going to explain how each step works because you could have just easily coded this by following their steps without even thinking how to do it all right so here's what's happening we're creating an empty list we loop through each element in that list so let's say the first element is actually a list so it would be better to call this variable list in lists rather than numbers because that's silly so the first time we loop through uh n which is these lists right over here right this is a list containing lists so the first time we loop through it okay let's just let's just walk it walk through it okay i'm gonna we call n here all right that's n we pass that into our uh lists variable and our argument now we have pass in list of lists again this function took only one argument keep that in mind this whole thing is just one list uh containing multiple lists inside of it result boom we initialized it to an empty list so what we did is we did that okay list is not that thing so for list in that the first element is a list one two three and list is one two three here as well number the first time we loop through is one and one we append one right here then the second time number is two it's two we append two right here then it's three right this three right here three three we append three loop ends what do we do we go to the outer loop we change these variables back to what they were number list number lists or sorry that's list this is list okay now our inner loop ended but our outer loop did not end because it only went through the first element we still have one more element left the second element in that is this guy here so the first number in that list is four four we append we go up here we append four then the second element in that guy is five we go and we append five then the next one is six we append six um seven results out of pen seven eight right results dot append eight and then it's nine and results data pen nine and it says return results so this function actually gets evaluated to that and then we call the print function on that list and uh you see that output on the screen okay so if i ran all of this make sure to put uh parentheses there and if i ran all of this you see a list one two three four five six seven eight nine that's how it actually worked now why i said there's an easier way to do this is what i would do is very simple i would say instead of doing an inner loop i would just say results that append um this gets a little bit complicated but so i'm not going to do it but it would essentially be uh element at index 0 plus the element at index 1. um so element at current index basically plus element at uh next index so what that means is in this giant list here it would do current index so let's say our index was at zero because this is this list is at position zero and this list um this element here is at position one so what it would essentially do is that plus that and this would automatically give you one list containing that okay and and it would just add it okay and then at the end you could do results of one or results of zero and get the final result but that just actually that complicates it a little bit um however um that would save you kind of going through another inner loop but you would have to do a different kind of loop for what i was saying because you need access to the indices so you actually need to do a loop with the range function okay so hopefully you understand how that worked let's take a look at our course page because we're done with lists and functions we're at 52 percent slowly but surely we're getting there and we're going to be at a really fun part in the next video we're going to be going through battleship and how to create an actual game how great is that then we have unit 8 left unit 9 is going to be awesome unit 10 i was thinking about skipping it but i'm gonna do it last because it's not useful so we're gonna actually get to more fun parts first that's what that means for you other than that that's it for this video guys you're watching um codecademy part 16 right this is kazi from clevverprogrammer.com please subscribe please like the video if it really helped you share it with whoever you want or maybe don't share it maybe that's your secret way of getting better at programming whatever's the case i hope you're enjoying yourself more than anything else and then i hope you're learning as well have a good rest of the week and i will see you in the next video codecademy part 17 okay take care\n"