List Functions | Python | Tutorial 12

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome to draft Academy my name is Mike in this tutorial I want to talk to you guys about using functions with lists in Python when python a list is one of the most important structures where we can store our information a list basically allows you to take a bunch of different values and organize them and store them inside of their own little list structure so down here in my program you can see i have two lists i have a list of lucky numbers and this is just a bunch of different numbers and then i have a list of friends so this could be like a list of somebody's friends and it's basically just a bunch of strings with names in them so these two lists are storing a bunch of different values and what makes list awesome is not only can I store like this many values I mean I could store hundreds or thousands or millions of values and when you're working with a lot of data and a lot of information of Python these lists are invaluable so because lists are so important there's actually a bunch of different functions that we can use with lists and those functions allow us to do things like modify the lists and get information about the lists so in this tutorial I just want to show you a bunch of those functions we're going to talk about some of the most common and the most popular functions that you would use on lists to make them more powerful and make them more easy to use so the first thing we can do is obviously just print out a list so down here I'm printing out this friends list and when I wonder and when I run my program you see down here we're just printing out all the elements in the list so it's always good to be able to print out all the elements inside of a list but there's a lot of other functions that we can use so one function is called the extend function and the extend function will basically allow you to take a list and append another list on to the end of it so I could say like friends dot extend and inside of these parentheses I can pass in the list that I want to add on to the friends list so I could say like lucky numbers and now when I print out this friends list not only is it going to have all the elements in the friends list it's also going to have all the elements inside lucky numbers list you can see down here we have all of our friends and then we also have the numbers over here so that's a really easy way to essentially just like add two lists together we can also just add individual elements on to a list so I could come down here and I could say friends dot append and this will actually allow me to append another item on to the end of this list so over here I could just add another name so we could say creed for example and now I'm like adding another friend on to the end of this list so when I run the program in addition to having the original items that were in the list up here we also have this additional item at the end and that append function is always gonna add the item onto the end of the list but let's say that you wanted to add an item into the middle of the list so like right here or right here there's another list function called insert and insert is going to take two parameters the first parameter is going to be the index where you want to insert the item so why don't we insert it here at index position one I'm just gonna say one and then I'm gonna type in the name of the element that I want to add so we can just say like Cowie and now at index position one we're gonna have the value Kellie and all of these other values are going to get pushed up they're gonna get pushed to the right one index position so now when I run the program you can see we have Kevin this new element Kellie and then all the other elements have been pushed off to the right we can also remove elements so for example let's say I wanted to remove this Jim value all I have to do is just say friends dot remove and I can just type in whatever element I want to remove so I want to remove this Jim element and so now when we run the program Jim is gonna be gone from the list and we just have these four guys left you can also remove all of the elements from the list so if I just wanted to completely like reset the list and get rid of everything I could just say friends got clear and this is gonna give us an empty list so to get rid of every single element inside the list there's also another really cool method which is called pop and you can just type out pop like that and basically what pops gonna do is it's gonna pop an item off of this list so when I click the play button you'll see here that we're left with kevin caron jim and oscar so we basically got rid of the last element inside the list and that's what pop does it pops an element off of the list so it basically removes the last element in the list so we have our list but let's say that we wanted to figure out if a certain element was in this list right if a certain value is in here so let's say I wanted to check to see if my friend Kevin was in this list well I could say friends dot index and in here I can just pass a value so I can say like Kevin and this is gonna tell me the index of Kevin so it'll tell me if Kevin's in the list so now when I click play you can see down here it says zero if I was to say like Oscar in here for example now it'll give me the index of Oscar so that's it three but if I put a name that wasn't in here so like if I put Mike that's not in the list and so now we're gonna get back in error because it says Mike is not in the list so that's an easy way to figure out if a specific element is inside of the list and if it is then you'll know what index it's at you can also count the number of similar elements in the list so I'm actually gonna come down here copy this gym element and we'll add another gym element into here so I could come down here and I could say friends dot count and we'll pass in gym and this will tell me how many times the value Jim shows up inside of this list so it should say two which it does down there because we have two values with the text Jim and so we have all these values we could also sort this list so I could come over here and I could say friends dot sort and what this will do is it'll sort the list in ascending order so we have a bunch of text here and you'll see what it does basically what it's going to do is it's going to put it alphabetical order so you can see we have JK o and T so the list went from being just random order like whatever we had up here to now it's in alphabetical order and that's also gonna work for lucky numbers so I could say like lucky numbers dot sort and then we'll print out lucky numbers and these should now be in ascending order which I'm realizing they already are up here but if I was to take this 42 and put it down here now it will figure everything out and put it in a sending order anyway we can also reverse a list so if I wanted I could reverse this not lucky numbers list I could say lucky numbers dot reverse and now when we print it out instead of being 42 8 15 16 23 it's gonna be 23 16 15 8 42 so it's basically just reverse the order of the list and finally there's one more list function I want to show you guys which is copy so I could actually create another list and I could make it as a copy so I could say like friends too and we could set this equal to friends copy and now friends 2 is gonna have all the same attributes as friends because it's basically just like copied it basically took a copy of that friends list so you can see it's the same exact thing and copying list can come in handy in a lot of different situations so those are just some basic list functions and just kind of like you know get your feet wet and working with lists obviously like the more you work with them the more you practice using these different functions the more comfortable you're gonna get but that's just kind of like a basic overview of what you can do hey thanks for watching if you enjoyed the video please leave a like and subscribe to drop acad to be the first to know when we released new content also we're always looking to improve so if you have any constructive criticism or questions or anything leave a comment below finally if you're enjoying chopped Academy and you want to help us grow head over to Draft Kadim e-comm forward slash contribute and invest in our future
Info
Channel: Mike Dane
Views: 33,140
Rating: undefined out of 5
Keywords: Programming
Id: MKpVFaeT6uk
Channel Id: undefined
Length: 8min 33sec (513 seconds)
Published: Sat Oct 21 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.