Lists are used to store multiple items in a single variable. They are one of the built-in data types in Python used to store collections of data. In a list, items are ordered and changeable, and duplicate values are allowed.
List items can be of any data type. List items are indexed; the first item has index [0], the second item has index [1], etc.
Determining the length of a list is essential for tasks such as loop control and input validation. Python provides the len() function, a built-in tool, for this purpose, alongside alternative methodologies.
How to Find the Length of a List in Python using len()?
Python has the built-in len() function to get the number of elements in a list. The function takes a list as an argument and returns an integer representing the number of elements within it.
1. How to find the length of an integer list using the built-in function len()?
my_list = [1,2,3,4,5,6,7,8,9]
print(len(my_list))
2. How to find the length of a string list using the built-in function len()?
Hosting_List = ["Cheap VPS hosting","Forex VPS server","Web Hosting","Windows VPS","Wordpress Hosting","Dedicated server Hosting"]
print(“The length of the list is”,len(Hosting_List))
print(‘The list consists of the following items:’, Hosting_List);
3. How to Find the Length of an Empty List?
If the list is empty, len() will return 0.
empty_list = []
print(len(empty_list))
4. How to Find the Nested List Length Using len()?
It's important to note that len() only counts the top-level elements in a list. If a list contains other lists (nested lists), they are counted as single elements.
nested_list = [[1, 2], [3, 4], [5, 6]]
print(len(nested_list))
If you want to count all elements inside nested lists, you need to use a more advanced approach, such as recursion or any other methods:
5. How to Find the Length of All the Elements in a List?
You can find the length of all the items of a list using Iteration:
The iteration method is another easy to use and reliable than any other built-in method.
flat_list = list(chain.from_iterable(nested_list)) # Flatten one level
print(sum(len(i) if isinstance(i, list) else 1 for i in flat_list)) # Count elements
Alternative Ways to Get List Length
1. How to Find the Length of a List Using length_hint() Method?
The length_hint() is also a built-in method and works where the len() doesn’t work.
from operator import length_hint
nested_list = [[1, 2], [3, 4, 5], [6, 7, 8, 9]]
total_length = sum(length_hint(sublist) for sublist in nested_list)
print("Total number of elements inside the nested list:", total_length)
2. How to Find the Length of a List Using a “For” Loop?
Manual iteration using loops can also determine list lengths. While len() is generally more efficient for standard lists, loop-based approaches are useful for understanding list traversal and for specific scenarios like nested list element counting.
my_list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]
count = 0
for sublist in my_list:
count += 1 # Add the length of each sublist
print("Total number of elements:", count)
3. How to Find the Length of a Nested List Using a “For” Loop?
While in the above method, we used loops to find the outer elements of the list, but this method gives the length of every element of a list.
nested_list = [[1, 2], [3, 4], [5, 6]]
count = 0
for sublist in nested_list:
count += len(sublist) # Add the length of each sublist
print("Total number of elements:", count)
4. How to Find the Length of a List Using a “While” Loop?
You can either use “for” or “while” to find the length of a list.
my_list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]
count = 0
index = 0
while index < len(my_list):
count += 1
index += 1
print("The length of the list is:", count)
5. How to Find the Length of Every Item Using a While Loop?
nested_list = [[1, 2], [3, 4], [5, 6]]
count = 0
index = 0
while index < len(nested_list):
count += len(nested_list[index]) # Add the length of each sublist
index += 1 # Move to the next sublist
print("Total number of elements:", count)
6. How to Find the Length of Sublists(separately) in Python?
This prints the length of each sublist separately.
nested_list = [[1, 2,7], [3, 4,8], [5, 6]]
for i, sublist in enumerate(nested_list):
print(f”Length of sublist {i + 1}: {len(sublist)}")
The ‘f’ before the string allows embedding variables or expressions inside brackets.
Although len() is the most efficient and recommended way, it cannot deal with nested lists. You can manually determine the length of the Lists and Sublists using the alternate ways mentioned in this tutorial.