In this Python post, we’ll learn how to get Python list size using top 2 methods. We’ll be using multiple examples to understand how we can get the size of list using multiple ways.
By the end of this post, you’ll be able to get the Python list size using 2 different methods.
So let’s not wait anymore and just get right into its implementation phase.
What is Python List Size?
It is actually the length of list in Python. Let’s say we have 5 items in our Python list, that means the size of list will be 5. But what if we have a list with larger amount of item. How’d we then get the length of our Python list?
Don’t worry, in the below section, we will practically explain it using 2 methods with each having its own example.
Getting Python List Size (2 Methods with Examples)
These 2 methods are listed below:
- Using len() function
- Using for loop
Let’s start explaining these methods one by one with practical examples.
Using len() function
It is actually the best way to get the length of Python list. See below code for its practical example:
pythonLst=[4,2,7,9,'blue',5.3,True,(3,5.5,'red'),34,'purple',['white',54,6.2]] print( len(pythonLst) )
Output
11
You can see that we have used different type of value in our Python list, these are integers, double, string, Boolean, tuple and list. You can see that the length of list is successfully fetched using the len(nameOfList) method.
The list and tuple that we have used inside our main list will be taken as one item, so the list will be taken as one value and the tuple will also be used as just one value.
If you want that each item of these lists are used as a separate value then you have to first add both of them to that list using the extend method. Click here to understand how its done properly.
Using for loop
We can use for loop as well to get the length of list. Let’s practically see in the below code that how can we get the length of list in Python.
pythonLst = [4,2,7,9,'blue',5.3,True,(3,5.5,'red'),34,'purple',['white',54,6.2]] sizeOfList = 0 for i in pythonLst: sizeOfList = sizeOfList + 1 print( sizeOfList )
Output
11
As you can see that the output is same which means its working just fine.
We have used the same above list, then we have created an integer with value 0. After that, we have used loop and increment the value by 1 whenever the loop runs. So the loop will run until every item of Python list is covered, then it will come out of loop and the print statement will run in which the Python list size will be printed.
So this is how you can easily fetch Python list size. You can use any one of these 2 methods but using len() method is preferable if you just want to get the size of Python list using one line.
Do ask if you still have any questions regarding Python list size. I’d be more than happy to answer all your questions.
Conclusion
To conclude this post, we have discussed 2 methods on how to easily get Python list size with proper and easy examples. I’d be looking forward at your valuable feedback on this post. Thank you for reading this article.