Open In App

Find size of a list in Python

Last Updated : 10 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A list is a collection data type that is ordered and changeable. A list can have duplicate entries as well. Here, the task is to find the number of entries in a list in Python.

Examples:

Input: a = [1, 2, 3, 1, 2, 3]
Output: 6
Explanation: The output is 6 because the number of entries in the list a is also 6.

Find the Length of a List in Python

Below are the methods that we will cover in this article:

Find the size of the list using the len() method

The len() works in O(1) time as the list is an object and has a member to store its size. Below is a description of len() from Python docs.

Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).

Python3




# Python program to demonstrate working of len()
a = []
a.append("Hello")
a.append("Geeks")
a.append("For")
a.append("Geeks")
print("The length of list is: ", len(a))


Output

The length of list is:  4


Find the length of a list using the sum() function

Another approach is to use the built-in sum() function in combination with a generator expression. This allows you to find the size of a list by summing the number of elements in the list that meet a certain condition.

Python3




# list of numbers
numbers = [1, 2, 3, 1, 2, 3]
 
# find the size of the list
size = sum(1 for num in numbers)
 
# print the size of the list
print(size)


Output

6


This will output 6 because the list contains 6 elements.

Time complexity: The time complexity of the approach using the sum() function and a generator expression is O(n), where n is the length of the list.
Space complexity: The auxiliary space complexity of this approach is O(1) because the generator expression only requires a single variable to store the current element being processed.

Find the length of the list using for loop

In this way, we initialize a variable count and then we increment the variable through the loop, and by the end of the loop we get the length of the list in our count variable.

Python3




lst = [1,1,2,5,1,5,2,4,5]
count = 0
for i in lst:
  count += 1
print("The length of the lst is :",count)


Output

The length of the lst is : 9


Time Complexity: O(n)
Space Complexity: O(1)

Find the size of the list using the length_hint() method

The length_hint() function from the operator module to estimate the length of a list. However, please note that this function is not guaranteed to give you the exact size of the list, especially for standard Python lists.

The length_hint() the function provides a hint about the expected length of an iterable, but it might not be accurate for all types of iterables. Here’s your example:

Python3




from operator import length_hint
 
lst = ['Geeks', 'For', 'Geeks']
size = length_hint(lst)
print('The size of the size lst:',size)


Output

The size of the size lst: 3


Time Complexity: O(n)
Space Complexity: O(1)



Previous Article
Next Article

Similar Reads

Change the label size and tick label size of colorbar using Matplotlib in Python
In this article, we will learn how to change the label size and tick label size of colorbar in Matplotlib using Python. Labels are a kind of assigning name that can be applied to any node in the graph. They are a name only and so labels are either present or absent. To properly label a graph, helps to identify the x-axis and y-axis. Each tick mark
2 min read
Find the size of a list - Python
In Python, a list is a collection data type that can store elements in an ordered manner and can also have duplicate elements. The size of a list means the amount of memory (in bytes) occupied by a list object. In this article, we will learn various ways to get the size of a python list. 1.Using getsizeof() function: The getsizeof() function belong
2 min read
PyQt5 - How to adjust size of ComboBox according to the items size
In this article we will see how we can adjust the combo box according to the size of item which has maximum size among the item list. By default we use setGeometry method and setSize method to adjust the size but it will not automatically adjust the size according to the item size. In order to adjust the size according to the maximum length item si
2 min read
Break a list into chunks of size N in Python
In this article, we will cover how we split a list into evenly sized chunks in Python. Below are the methods that we will cover: Using yieldUsing for loop in PythonUsing List comprehensionUsing NumpyUsing itertoolMethod 1: Break a list into chunks of size N in Python using yield keyword The yield keyword enables a function to come back where it lef
5 min read
Python | Combinations of elements till size N in list
The problem of finding the combinations of list elements of specific size has been discussed. But sometimes, we require more and we wish to have all the combinations of elements of all sizes till N. Let's discuss certain ways in which this function can be performed. Method #1 : Using list comprehension + combinations() This task can be performed us
9 min read
Python | Subgroups of i'th index size in list
Sometimes we need to group elements and grouping techniques and requirements vary accordingly. One such way to group the elements is by the i'th size in list which stores the dictionary of index keys with values as list elements of subsequent size i. Input : [4, 7, 8, 10, 12, 15, 13, 17, 14, 5] Output: {1: [4], 2: [7, 8], 3: [10, 12, 15], 4: [13, 1
3 min read
Python | Sort list of lists by the size of sublists
Given a list of lists, the task is to sort a list on the basis of size of sublists. Let's discuss a few methods to do the same. Method #1: Using sort C/C++ Code # Python code to demonstrate # sort list of list # on the basis of size of sublist ini_list = [[1, 2, 3], [1, 2], [1, 2, 3, 4], [1, 2, 3, 4, 5], [2, 4, 6]] # printing initial ini_list print
5 min read
Python | Categorize the given list by string size
Sometimes, we have a use case in which we need to perform the grouping of strings by various factors, like first letter or any other factor. These type of problems are typical to database queries and hence can occur in web development while programming. This article focuses on one such grouping by size of string. Let's discuss certain ways in which
6 min read
Python | Increase list size by padding each element by N
Given a list and an integer N, write a Python program to increase the size of the list by padding each element by N. Examples: Input : lst = [1, 2, 3] N = 3 Output : [1, 1, 1, 2, 2, 2, 3, 3, 3] Input : lst = ['cats', 'dogs'] N = 2 Output : ['cats', 'cats', 'dogs', 'dogs'] Approach #1 : List comprehension C/C++ Code # Python3 program to increase lis
2 min read
Python | Size Range Combinations in list
The problem of finding the combinations of list elements of specific size has been discussed. But sometimes, we require more and we wish to have all the combinations of elements of all sizes in range between i and j. Let’s discuss certain ways in which this function can be performed. Method #1 : Using list comprehension + combinations() This task c
4 min read
Practice Tags :