
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How do we get the size of a list in Python?
List is one of the most commonly used data structures provided by python. A list is a data structure in python that is mutable and has ordered sequence of elements. Following is a list of integer values.
Example
lis= [25,52,23,34,45] print(lis)
Output
If you execute the above snippet, it produces the following output.
[25, 52, 23, 34, 45]
In this article we will see different ways to the number of elements in a list in Python.
Using len() function
Using len() function is one of the simplest method to find the size of the list. This function works in O(1) where it returns the size(number of elements) of an object.
Example
In the following we used len() built-in function to find the size of a list.
lst = ["Tutorialspoint", "is", "the", "best", "platform", "to", "learn", "new", "skills"] length = len(lst) print("The size of a list is:",length)
Output
On executing the above program, the output produced is as follows.
The size of a list is: 9
Using python loops
To find the length of a list in python, we can use a loop to achieve it. This is a traditional or a naïve method. The basic algorithm of this method is as follows −
Declare the counter variable and give it a value of zero.
Traverse the elements of the list using the for loop, incrementing the counter variable by one after each element.
As a result, the length of the lists will be saved in the counter variable, which you may retrieve by representing it.
Example
The following is an example code to find the size of a list by using the traditional method.
lst = ["Tutorialspoint", "is", "the", "best", "platform", "to", "learn", "new", "skills"] count = 0 for i in lst: count = count+1 print("The size of a list is:", count)
Output
On executing the above program, the output produced is as follows.
The size of a list is: 9
Using length_hint() function
To find the size of the lists, python includes a built in operator called length_hint(). The variable is passed as an argument to the operator.length.hint() method, which stores the data. Use the "import" keyword to import the length.hint() method from the built in operator library.
Example
The following is an example code to find the size of a list by using the length.hint() function.
from operator import length_hint lst = ["Tutorialspoint", "is", "the", "best", "platform", "to", "learn", "new", "skills"] length = length_hint(lst) print("The size of a list is:", length)
Output
On executing the above program, the output produced is as follows.
The size of a list is: 9
Using sys module
The getsizeof() function in Python is available in the sys module. The function takes an object and calls the __sizeof()__ method, which returns the amount of memory currently occupied by the data structure together with any overhead incurred by the garbage collector if the object is handled by one.
Because of this, the size of the list received using the __sizeof()__ method is smaller than the size obtained with the getsizeof() function. All of the objects in memory are tracked using the trash collector.
Example
We first built two lists for this example. 5 integers make up "l1," whereas mixed datatypes make up "l2." We passed the list object to the getsizeof() method in order to find out how big the list is, and the function's execution returned the list's size in bytes along with garbage collector overhead.
import sys # creating the list and initializing it l1 = [4,9,46,83,26] l2 = [5.4, "Python", "Coding", 76.4] print ("Size of l1 is: ", sys.getsizeof(l1)) print ("Size of l2 is: ", sys.getsizeof(l2))
Output
Following is an output of the above code −
Size of l1 is: 104 Size of l2 is: 96
- Related Articles
- How do we get size of a list in Python?
- How to get the size of a list in Python?
- How do I get list of methods in a Python class?
- How do we specify the buffer size when opening a file in Python?
- How do I set the size of a list in Java?
- How do I find the size of a Java list?
- How to get the size of a string in Python?
- How do we create python string from list?
- How do I get a list of all instances of a given class in Python?
- How do you get the file size in C#?
- Find size of a list in Python
- How do we get food?
- How do we add a menu list in HTML?
- How do we define the start of a term in a definition list in HTML?
- How to get the last element of a list in Python?
