In this Python tutorial, we will see how to find the size of a list in Python. Here we will see several different methods to find the size of a Python list.
Lists are one of the basic data structures in Python and are used to store multiple items in a single variable. Sometimes, it becomes crucial to know the size of a list, i.e., how many elements it contains.
For example, we have a list of the names of some MLB teams. To know the total number of the teams in MLB, we just have to find the size of the list.
mlb_teams = ['New York Yankees', 'Boston Red Sox', 'Los Angeles Dodgers', 'Chicago Cubs', 'San Francisco Giants']
Finding the size of a Python list
There are so many ways to find the size of a list in Python. They are:
- Using len() function
- Using length_hint() function
- Using For loop
- Using generator expression with sum() method
Method-1: Using Python len() function to find the size of a Python list
In Python, the len() function is used to find out how many items (the length) a list or other iterable contains. Here, we will simply use len() and will print the output.
This is a list of all US states, ordered alphabetically, to know how many total states are in the US:
us_states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'] print('Total number of US states are: \n', len(us_states))
The output is:
Total number of US states are: 50
This way we find the size of the list in Python using len(). This is the most common method to find the length of a Python list.
Method-2: Using Python length_hint() to find the size of a Python list
The Python operator module has a length_hint() method to estimate the length of a given iterable object. Here, firstly we will import the length_hint method from the operator module, and simply use the length_hind() and will print the result.
In this Python list, we were surveying to collect the data for the most populated cities in the US, but we forget the number of names we put. To get the number we will find the size of the Python list.
from operator import length_hint top_us_cities = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix'] size = length_hint(top_us_cities) print(size)
The output is:
This way we can use the length_hint() method from the operator module to find the size of a Python list;
Method-3: Using for loop to find the size of a Python list
This is a more manual approach in Python List. Here, we will create a counter variable to keep the count on and will increment it with each time the for loop, loops over the list in Python.
In this, we have different state’s names to visit this month:
states = ['New York', 'California', 'Texas'] counter = 0 for i in states: counter += 1 print('Numbers of states we are visiting is: ', counter)
The output is:
Numbers of states we are visiting is: 3
This way we can use For loop to find the size of the Python list.
Method-4: Using generator expression with sum() method to find the size of a Python list
This method is similar to the Python for loop method but uses a generator expression, a more “Pythonic” way to handle lists. Here, we’re creating a new iterable of 1’s with the same length as the original list, and then summing it up to get the size using the sum() method.
In this we have the list includes some of the top tech companies in the United States. Let’s find out how many are there.
us_tech_companies = ['Apple', 'Google', 'Microsoft', 'Amazon', 'Facebook'] Number = sum(1 for _ in us_tech_companies) print('Number of top US companies is: ', Number)
The output is:
Number of top US companies is: 5
Note: We can use List comprehension also to create an iterable.
This way we can use a generator expression with the sum() method to find the size of a Python list.
We have seen many different ways to find the size of the Python list like len(), length_hint(), for loop, and generator expression with sum(). We must choose the correct method according to our needs. The len() function is most commonly used to find the length/size of the Python list.
You may like to read:
- How to return multiple values from a Python list
- How to find the sum of the list in Python
- How to remove duplicate elements from a List in Python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.