As a seasoned Python developer and data analyst, the len() function is one of my favorite parts of Python. It‘s so simple, yet amazingly useful!
In this comprehensive guide, I‘ll cover all key aspects of Python‘s len() function so you can master everything about this built-in function.
What Does len() Do in Python?
The len() function gives the total count of items present in an iterable object. For example:
fruits = [‘apple‘, ‘banana‘, ‘mango‘]
print(len(fruits)) # 3
Here, len() returned 3 – the number of elements in the fruits list.
The key thing is it can quickly tell you the size of lists, tuples, strings, dicts – basically any iterable in Python!
The Syntax of len()
Here is the simple syntax for the len() function:
len(iterable)
It accepts only one required parameter:
iterable– Any iterable object like lists, strings, dicts, sets, etc.
And it returns an integer value equal to the number of items present in the iterable.
That‘s the basic syntax. Now let‘s see how to use len() with different data structures in Python.
Using len() with Python Data Structures
One of the most common uses of len() is to get the length of sequences and collection objects in Python. Let‘s go over some examples.
len() with Lists
Lists are one of the most frequently used data types in Python. To get the total elements in a list, use len():
numbers = [1, 5, 2, 9, 7]
number_count = len(numbers)
print(number_count) # 5
We can also combine len() directly inside the print() function:
print(len([1, 5, 2, 9, 7])) # 5
So len() can instantly give you the total items in a Python list.
len() with Tuples
Tuples are immutable sequences in Python. The syntax to find the tuple length is the same:
fruits = (‘apple‘, ‘banana‘, ‘mango‘)
print(len(fruits)) # 3
So for ordered sequence data types like lists and tuples, len() returns the count of elements.
len() with Strings
In Python, strings are sequences of Unicode characters. To find the length of a string, use:
name = ‘John‘
print(len(name)) # 4
We can also combine len() with string functions like lower():
print(len(‘JOHN‘.lower())) # 4
This makes len() super useful when working with Python strings.
len() with Dictionaries
Dictionaries are unordered collections of key-value pairs in Python.
To get the size of a dictionary, use len() on the dict object:
student_scores = {‘John‘: 90, ‘Mary‘: 78, ‘Steve‘:82}
print(len(student_scores)) # 3
Here, the dictionary has 3 key-value pairs, so len() returned 3.
len() with Sets
Sets are unordered collections of unique elements in Python. We can use len() to get the set size:
colors = {‘red‘, ‘blue‘, ‘green‘}
print(len(colors)) # 3
Although sets are unordered, len() easily gives us the count of items.
So in summary, len() can be used to get the lengths of any built-in Python sequence and collection objects.
Common Applications of len() in Python
Beyond just getting the lengths of data structures, the len() function has several common applications:
Looping using len()
We can use len() in loops to iterate through the elements of an iterable.
With for loop:
fruits = [‘apple‘, ‘banana‘, ‘mango‘]
for i in range(len(fruits)):
print(fruits[i])
Here, we looped from 0 to the length of the fruits list.
With while loop:
names = [‘John‘, ‘Mary‘, ‘Steve‘]
while len(names) > 0:
print(names.pop())
We checked the len() on each iteration to keep looping until the list becomes empty.
So len() comes in handy when writing loops in Python.
Conditional Checking
We can use len() for validating string lengths and collection sizes in Python.
For example:
text = ‘Python programming is awesome‘
if len(text) < 10:
print(‘Text too short‘)
elif len(text) > 50:
print(‘Text too long‘)
else:
print(‘Text length validated!‘)
Here we checked if the text length is acceptable.
Similarly, we can validate list and dict sizes:
users = [{‘name‘: ‘John‘, ‘age‘: 20}, {‘name‘: ‘Mary‘, ‘age‘: 22}]
if len(users) < 1:
print(‘No users!‘)
elif len(users) > 5:
print(‘Too many users‘)
else:
print(f"{len(users)} users found")
So len() makes validation concise.
Custom Sorting
We can use len() as the key function while sorting iterables.
For example, to sort a list of strings by length:
fruits = [‘banana‘, ‘kiwi‘, ‘apple‘, ‘orange‘]
fruits.sort(key=len)
print(fruits)
# [‘kiwi‘, ‘apple‘, ‘banana‘, ‘orange‘]
By passing len as the key function, it sorted by string length.
Comprehensions
We can leverage len() inside list, dict and set comprehensions for some useful applications.
List Comprehension
Let‘s extract only short names from a list:
names = [‘John‘, ‘Mary‘, ‘Tim‘, ‘Sam‘, ‘Bob‘]
short_names = [name for name in names if len(name) < 4]
print(short_names)
# [‘Tim‘, ‘Sam‘]
Dictionary Comprehension
We can create a dict mapping string to length:
fruits = [‘apple‘, ‘banana‘, ‘kiwi‘]
fruit_len = {fruit: len(fruit) for fruit in fruits}
print(fruit_len)
# {‘apple‘: 5, ‘banana‘: 6, ‘kiwi‘: 4}
So len() helps write concise and effective comprehensions.
NumPy and Pandas
len() can also be used on NumPy arrays and Pandas data structures.
NumPy example:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(len(arr)) # 2
print(arr.shape) # (2, 3)
For Pandas DataFrame:
import pandas as pd
df = pd.DataFrame({‘Name‘: [‘John‘, ‘Mary‘], ‘Age‘: [20, 21]})
print(len(df)) # 2
So len() works with popular data analysis libraries too.
As we saw, len() has quite a diverse range of applications! Now let‘s talk about best practices.
Best Practices for len()
Although a simple function, there are some best practices worth keeping in mind:
-
Always pass an iterable object to
len(). Avoid non-iterable types like integers or floats to avoid errors. -
For multi-dimensional NumPy arrays, prefer using
ndarray.shapeoverlen(). It gives more clarity. -
Use
len()on the DataFrame itself carefully. Length of rows may be more appropriate for many cases. -
With generators, first convert to a list before using
len(). Generator objects themselves do not have a length. -
Make sure single element tuples use a trailing comma. For example,
t = (2,). -
Avoid using
len()for cases where you just need to check if an object is empty or not. Use the preferred Pythonic way of simply checkingif some_list:orif some_dict:
Following these best practices will help you avoid mistakes and write better code using len()!
len() Usage in Data Analysis
As a data analyst, I find myself using len() often when loading, exploring and transforming data.
Here are some examples of how I leverage len() for data tasks:
- Quickly validate if a CSV dataset loaded correctly by checking
len(df) - Checking for null values using
len(df.columns[df.isnull().any()]) - Validating if a text column has expected length of strings
- Making sure encoded categorical variables match original using
len() - Splitting data into train/test sets using
len()forindexing - And many more!
So while a simple built-in function, len() has so many uses when writing data transformation and analysis code in Python.
Summary of Python‘s len()
To wrap up, here are some key pointers about the len() function:
- Returns total count of elements in an iterable object
- Works on sequences like lists, tuples, strings and collections like dictionaries and sets
- Commonly used for loops, conditions, sorting, comprehensions, NumPy/Pandas
- Accepts only iterables objects, avoid passing non-iterable types
- For multi-dimensional arrays, prefer using
ndarray.shape - Follow best practices like making single element tuples properly
I hope this comprehensive guide helped you master Python‘s len() function! It may be a small utility, but extremely handy.
Let me know in the comments if you have any other creative uses of len() in your code!