In this tutorial, we will learn how to find the length of a list in Python. Lists are one of the most commonly used data structures in Python, and knowing how to find their length is an essential skill for any Python developer.
Whether you’re a beginner or an experienced developer, this tutorial will teach you the easiest way to determine the length of any list in Python.
Step 1: Understand Lists in Python
In Python, a list is a collection of ordered elements. It can store elements of any data type, such as strings, integers, floats, and more. You can create a list by placing a sequence of elements inside square brackets []
, separated by commas. Here’s an example of a list with integer elements:
1 |
numbers = [1, 2, 3, 4, 5] |
A list can be of any length, even an empty one, like this:
1 |
empty_list = [] |
Step 2: Use the len() Function
To find the length of a list in Python, you can use the built-in function len(). This function takes a single argument, which is the list for which you want to find the length.
Here’s the syntax of the len() function:
1 |
len(list) |
Let’s take an example to understand how it works.
1 2 3 |
names = ["Alice", "Bob", "Charlie", "David"] list_length = len(names) print(list_length) |
4
In the example above, we created a list named names
with four elements. Then, we used the len() function to find its length, and stored it in a variable called list_length
. When we printed the value of list_length
, we got the output 4, which is the correct length of the list.
Step 3: Find the Length of Nested Lists
Finding the length of a nested list (a list inside another list) is also quite simple with the len() function. You just need to pass the nested list as an argument to the function. Here’s an example:
1 2 3 4 5 6 7 8 9 10 11 |
nested_list = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] outer_list_length = len(nested_list) inner_list_length = len(nested_list[0]) print("Length of the outer list:", outer_list_length) print("Length of the inner list:", inner_list_length) |
Length of the outer list: 3 Length of the inner list: 3
In this example, nested_list
is a list containing three sublists. Each sublist has three elements. When we used the len() function, we were able to find the length of both the outer list (which is 3) and the inner list (also 3) by passing them as arguments to the function.
Full Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
numbers = [1, 2, 3, 4, 5] empty_list = [] names = ["Alice", "Bob", "Charlie", "David"] nested_list = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] list_length = len(names) outer_list_length = len(nested_list) inner_list_length = len(nested_list[0]) print("Length of the names list:", list_length) print("Length of the outer list:", outer_list_length) print("Length of the inner list:", inner_list_length) |
Length of the names list: 4 Length of the outer list: 3 Length of the inner list: 3
Conclusion
In this tutorial, we have learned how to find the length of a list in Python using the len() function. This is a simple yet powerful technique that you can apply to any list, irrespective of the type of elements it contains or its structure. Keep practicing and experimenting with different lists to master this skill!