How To Check If A List Is Empty In Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is up everybody my name is zach welcome back to case digital in today's video we are answering the question of how to check if a list is empty or not in python so without further ado let's hop right in and start coding what is up everybody so like i mentioned in this video we're trying to figure out ways to check whether or not a list is empty in python and i'm going to give you two methods to do so one the first one that i thought of method number one is kind of the one that i i just intuitive like if i was thinking where to think about this i was like this is how i probably do it um the method number two is based on just me looking out there and just seeing like hey like is there a better way or other ways that could do this and this is one of the ways that i thought was kind of cool that i thought i'd show but before we start let's talk about our setup our setup is essentially um if you've seen some of my previous videos i've just been using this current list that's just one through four now i add another one that's called just an empty list there's nothing in it just so we can use this and i'm gonna do i'm gonna run these functions on both of them and it should tell us like it should tell us that this one's not empty and it has something in it and it should tell us that this one is empty so the first method the one that i've kind of thought of is essentially just checking if the length of the list is equal to zero and what i do did is you could do like an if statement if this is if you're trying to capture this around an if statement or if you need to assign this to a variable and use that which is kind of the example i'm running with here that's what i did i essentially said here's my variable is empty and i said the length of the list and then i just use this boolean expression to say that if it's equal to zero and this if the length of the list is like four equals equals zero obviously four does not equal zero so it'll say that hey this is false so if i were to run this through it this should tell us like in this part right here it should say that hey this is not empty method number one for using this list is not empty whereas method number two for using this list is empty so let's go ahead and run that and i'll run it right here and essentially you'll see that hey method number one is empty it's false and that's because yeah there's stuff in here so of course is empty is false um method number two or i mean list number two using an empty list of that method number one is empty yes this is true because there's nothing in there so that's essentially method number one that's kind of the way that i thought through it another way you could use this if you're trying to do this and you want to use the if statement like i said is you could say if the length of the list is not equal to or if the length of the list is equal to zero then print empty so if i run this you should see that i get and i'll just do this indent here so it makes it so we can read it better down below if i run it you should see that um yeah it did not print empty because the length of the list or this one is not zero there's stuff in it so but as you can see here it did so that's the two methods you can use if you wanted for this the kind of the way that i thought through it that if you just want to use it through an if statement to capture it or if you wanted to assign it to a variable that's how you could go about it so let's talk about method number two and this is probably the more pythonic way of doing it that people do and it's actually kind of cool so let's talk about method number two okay i just want to jump in real quick say thank you so much for watching the video so far if it's providing you value please click that like button below and if you haven't already please subscribe to my channel so we can learn more about software development and programming and well speaking of programming let's get right back to it okay so method number two is something that i found when i was just doing some research on this because i was trying to think is there a better way to do this is there a better way to check whether or not a list is empty and the method that i found is probably what i what people would refer to as the more pythonic way or the more concise way to do that in python and that's essentially using the booleanness is what they kind of like the booleanness or using a rule value of a list because what's interesting is is i can do this so kind of using the same logic here like this is the way you want to do it if you want to assign it to a variable but if you wanted to check if a the list is empty like if you're using like a control statement like an if stuff to say hey if it's empty um like then do something so in this case i'm gonna say if list then i want you to say like print not empty and i'll talk about this intersect so if we run this we should get that again like i mentioned before we should get the exact same results as what we did above and so and i'm gonna uncomment these so we can run it and we'll talk further about it now what i wanna show here is essentially like when i ran through this list so i have the list of one two three four when i do method number two oh and it shows number one so let me change this really quick and we'll rerun this and let me actually do that we'll run this really quick all right so this will make it more this will make more sense as we go through and look at this um so when i run through this list you should see that it says it's not empty and that's what we expected um but what i wanted to talk about about the booleanness is a lot of times i'll use this in like my programming stuff is i'll say hey if i just double check and see if there's anything in this object or in this list i'll do if list then i want you to do whatever i wanted to or however i need to use the contents of that list um and if you do not list that's basically saying the opposite saying like hey if not list meaning if there's nothing in there then do something and that's essentially what i'm doing up here when i'm assigning this to this variable this is basically saying like like i i'm not 100 for sure but i assume what's kind of happening is probably something similar to this on the internals of this so it's basically doing some buoy evaluation of saying hey like this list is empty there's nothing here like the about the length of it is zero like so um yeah do by doing it not this is going to say yeah that's by not this it's going to say yeah there's nothing in here so that means it's empty and so when you assign a variable to is empty it's meaning yep that if there's nothing in there it's going to be true and go ahead and do it otherwise if you do something like this like say if list if there is something in here then it will say hey there that means that there's you know there's values in that list um compared to the not values so basically that's the two methods this is the more pythonic way if that's a little confusing leave some um comments below and i'll do my best to answer them maybe explain this more but essentially by doing the using the booleans and doing a not in the list object it'll check whether or not it is empty so if this is is empty and we do a knot and there's actually something value you'll see like this does down but here it'll say it's false but if you pass an empty list in and use this knot it'll say yeah that is empty so that is true especially for like this list right here so those are the two different methods that you can use to check whether or not a list is empty in python um let me know what you think let me know how you would use these i could definitely see like i definitely the use cases i use is a lot more of using the if statements rather than a variable um because then sometimes it's just creating an extra variable that i don't really need but sometimes that's useful so play with it and let me know what you think in the comments below and which one you like better or which one you prefer hey thank you so much for programming with me today i really appreciate it and i hope this provided you value and how you can further your you know python knowledge and actually your understanding of lists specifically geared to how you can check whether or not a list is empty um using those two different methods whether it's using the booleanness of the list or if it's using kind of like they're just checking the count or the or the length of the list and if it's you know if it's zero then that means it's empty otherwise it means there's something in it and so let me know what you which method you like below in the comments and until next time keep on programming
Info
Channel: Case Digital
Views: 11
Rating: 5 out of 5
Keywords: Software, software developer, developer, python, python tutorial, programming, programmer, python programming, how to check if a list is empty in python, check if a list is empty python, python lists, python list, python empty list, how to tell if a list is empty in python, empty list python, list, lists, lists in python, how to determine if list is empty in python, ways to check for empty list in python
Id: VS9km2_ALIM
Channel Id: undefined
Length: 6min 42sec (402 seconds)
Published: Wed Sep 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.