This content originally appeared on DEV Community and was authored by hrishikesh1990
In this short tutorial, we look at how you can use Python to find list length. We look at all the various methods along with their pros and cons.
This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around often used or interesting concepts.
Table of Contents - Python list length
Python Lists - Index & length:
Lists are a collection of items of similar or different data types. Although a collection, these items are ordered and given a specific position called index. These indexes are used to retrieve a particular element.
Python list also has length. This denotes the size or number of items in a list and this is used to loop over and perform operations on the list.
Finding a Python lists length:
There are two methods that you can use in Python to find a list’s length. The first method uses the len() built-in function and is the most commonly used method. The other method finds the length using logic and is used only to familiarise yourself with the concept.
Let us look at the first method now.
Using len():
In this method, we use the len() function to find the length of the list. This function takes in iterable as arguments and returns its length. Since lists are iterable, len() can be used to find their length.
The syntax is as follows:
len(iterable)
Code to find Python list length using len():
list_1 = ["Hire", "the", "top", 1, "%", "freelance", "developers"]
print ("length of the list = ", len(list_1))
#Output - "length of the list = 7"
Note: These lists are small and one could easily identify the length. However, when you start working with larger lists you would need len() to find the length.
Using a counter:
As I mentioned earlier, using a counter in Python to find a list’s length is not commonly used. This is because len() does the same thing in a more simple manner. However, it is a good practice to know this method as well.
For this method, we use a for loop to count the number of items in a list. This method would work on any other iterable as well.
Code to find Python list length using a loop:
list_1 = ["Hire", "the", "top", 1, "%", "freelance", "developers"]
counter = 0
for i in list_1:
counter += 1
print ("length of the list = ", counter)
#Output - "length of the list = 7"
In this code, we have used a for loop to iterate over the list (for every item in the list the loop runs once).
We assign a variable called counter and have it increment every time the loop runs and since it runs for each item the count variable returns the number of items.
In case you do not clearly understand the increments part please feel free to look at this tutorial - Python increment.
Python list length - Closing thoughts
While comparing both these methods the len() operator is the goto method in Python to find the list length. Practice it a few times so that you get a solid understanding. As a next step, you could create a few lists, find the length of each and print the largest.
This content originally appeared on DEV Community and was authored by hrishikesh1990
hrishikesh1990 | Sciencx (2021-08-16T11:38:38+00:00) Find the Length of a Python list?. Retrieved from https://www.scien.cx/2021/08/16/find-the-length-of-a-python-list/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.