For all the self-taught geeks out there, here our content library with most of the learning materials we have produces throughout the years.
It makes sense to start learning by reading and watching videos about fundamentals and how things work.
Machine Learning Engineering (16 weeks)
Full-Stack Software Developer
Search from all Lessons
Curated list of small interactive and incremental exercises you can take to get better at any coding skill.
Curated section of projects to build while learning with simple instructions, videos, solutions and more.
Guides on different topics related to the technologies that we teach in our courses
Social & live learning
The most efficient way to learn: Join a cohort with classmates just like you, live streams, impromptu coding sessions, live tutorials with real experts, and stay motivated.
From zero to getting paid as a developer, learn the skills of today and tomorrow. Boost your professional career and be hired by a technology company.
A simple way to get the length of any Python list is using the
len() function as follows:
1countries = ["Canada", "Argentina", "Russia", "Mexico", "Germany"] 2 3countries_length = len(countries) 4 5print(countries_length) #Output: 5
This is a simple way to get the length of a Python list, let's dive into how the
len() function works and which other methods we can use to get the length of a list in Python in the following sections.
len() function is used to get the length of a Python list, you can pass a list to this function and it will return an integer indicating the size of the list as follows:
You can pass a list directly to this function like this:
1print(len(["John", "Bobby", "Janet"])) #Output: 3
Or you can also assign a list to a variable and pass that variable to the
len() function as follows:
1names = ["John", "Bobby", "Janet"] 2 3print(len(names)) #Output: 3
It is important to clarify that a list length is not the highest accessible index of that list. Remember that the first item's index is 0, let's use the above example to explain this:
1names = ["John", "Bobby", "Janet"] 2 3print(len(names)) #Output: 3 4 5print(names) #Output: Janet 6 7print(names) #Output: IndexError: list index out of range
See that if we print index #2 of the names list we'll get the last item that is
Janet, but if we try to print index #3 (same value as the list length) we'll get an Index error indicating that the list index is out of range. So we can say that the last accessible index on a list would be the list length - 1.
length_hint() method is another way to get the length of any list in Python, it basically works in the same way as the
len() function, the difference, in this case, is that the
length_hint() method is defined in the operator module, this means that to use this method, we must import it first.
After importing this method into our code, to use
length_hint() we simply have to pass our list to it as follows
length_hint(any_list). Let's see an example of how to import and use this method as follows:
1from operator import length_hint 2 3our_list = [1, 2, 3, 4, 5, 6] 4 5our_list_length = length_hint(our_list) 6 7print("The length of our list is: " + str(our_list_length)) #Output: The length of our list is: 6
You can also use the
length_hint() method to check the length of a string as follows:
1from operator import length_hint 2 3our_str = "Hello Rigoberto" 4 5our_str_length = length_hint(our_str) 6 7print("The length of our string is: " + str(our_str_length)) #Output: The length of our string is: 15
A third way to get the length of any list in Python is to use the Naive method. This method uses for loops to get the length of the desired list. To use this method, we have to follow some basic steps before getting the length of the desired list. First, we need to declare a counter variable and initialize this counter to 0. Second, we need to use a for loop to go through all elements of our list, with the condition to increase our counter variable by 1 each and every time the loop encounters an element of the list. Third, after looping through our list with the condition explained previously, our counter variable will give us the length of that list (we need to print the counter value to get the list length). Let's see an example of how to use the Naive method below:
1special_list = [10, 11, 12, "Dog", "Cat", "Bird"] 2length = 0 #This will be our counter 3 4for x in special_list: 5 length+=1 6print("The length of our special list is: " + str(length)) #Output: The length of our special list is: 6
In this example we first defined our list (
special_list), we then defined the counter variable and initialized it to 0 (
length), then we used a for loop to go through all the elements of the
special_list with the condition to increase our
length variable by 1 in this loop. Finally, we just needed to print our
length counter value to get the
As you can see, there are various ways how to get Python list length. If you are wondering which option to use, we recommend you stick with the
len() function since is the easiest and fastest way to get any list length. You'll also write less code and that is always a plus in the programming world since it is best to keep things simple.