Python is in high demand among software developers and data scientists around the world. This programming language has various data types and six of them in particular for storing sequences. These include List, Number, String, Dictionary, and Tuple. In this tutorial, we will understand what are Python lists and also look at how to find the length of a list in Python.
Python Lists Explained
Lists are considered the most reliable ways of saving items in a sequence in Python. They let you store a single variable with multiple items in it. Here is a simple example:
samplelist = [ “umbrella”, “raincoat”, “boots” ]
As you can see, you just have to use square brackets to create a list in Python. Moreover, the items are ordered in a definite series. You can include duplicate items in a Python list and also update items as per your needs. Each item of the list has an index, starting from 0. For more clarity, we have listed the starting index numbers below:
- First item: 0
- Second item: 1
- Third item: 2
In the context of the example mentioned earlier, the index number for the elements are:
- umbrella: 0
- raincoat: 1
- boots: 2
If you wanted to make any changes to the sample list, like add or remove specific elements, you would refer to these index numbers.
Lists differ from arrays as they offer a more compact and efficient way of storing a large number of elements. While arrays involve numerical operations, you do not have to perform a direct mathematical activity with lists.
Now, let us discuss how to determine the length or size of a list in Python.
Length of a list in Python
With length or size, we mean the number of items in the Python list. You can measure if with a simple len() function. Look at the following demonstration:
samplelist = [ “pen”, pencil”, “brush”, “notebook” ]
# Output: 4
This is the most conventional technique used by Python programmers to find a list’s length. However, there exist other methods as well. Let’s go into more detail.
- The Naive Method
This method involves running a loop and increasing the counter. This is done until the list’s last element. In the absence of other efficient strategies, this technique offers a convenient option to know the count of a Python list. Here’s how it works:
MyList = [ “Bye”, “Friends”, 4, 5, 6]
print (“This list is : ” + str(MyList))
counter = 0
for i in MyList:
counter = counter + 1
print (“Length of list with naive is: ” + are (counter))
This list is: [“Bye”, “Friends”, 4, 5, 6]
Length of list with naive is: 5
- The length_hint() approach
This method is relatively lesser known than the other two techniques. It is defined in the operator class in Python and tells us about the element count in the following manner.
First, you need to import the len() and length_hint functions from the operator. Then, implement the code given below.
#Sample code for length of list using length_hint
TestList = [“Winner”, 1, 2, 3, 4, 5]
print (“python list is: ” + str(test_list))
#Find length of python list
list_len = length_hint(TestList)
#Print length of list
print (“Length of list with length_hint method is: ” + str(list_len)
python list is: [“Winner”, 1, 2, 3, 4, 5]
Length of list with length_hint methods is: 6
With this, we have given you three different ways in which you can count the number of elements of a Python list, namely len() function, naive method, and length_hint technique. Here is a comparison of the three alternatives based on the time taken to deliver the list count in Python.
Once again, you need to import length_hint and time from the operator. After that, you can use this sample code to analyze the performance of each method:
# Initialize list
final_list = [1, 3, 5, 7, 9]
# Print test_list
print (“My python list is : ” + str(final_list))
# Find length of list using naive
# Initialize counter
begin_time_naive = time.time()
counter = 0
for i in final_list:
# increment the counter
counter = counter + 1
end_time_naive = str(time.time() – begin_time_naive)
# Find length of list with len()
begin_time_len = time.time()
list_len = len(final_list)
end_time_len = str(time.time() – start_time_len)
# Find length of list with length_hint()
begin_time_hint = time.time()
list_len_hint = length_hint(final_list)
end_time_hint = str(time.time() – begin_time_hint)
# Print time
print (“Time using naive is : ” + end_time_naive)
print (“Time using len() is : ” + end_time_len)
print (“Time using length_hint() is : ” + end_time_hint)
The output will show that the len() method is the fastest one out of the three ways of finding the size of list in Python.
The Way Forward
Python is an integral part of the data science curriculum at the postgraduate level due to its high industry demand. Whether you are a fresher, software or IT professional, or a data professional, you should be well-versed with this programming language.
Most upskilling programs aim to make candidates familiar with Python by covering the fundamentals of programming, statistical tools, and predictive analytics as a part of the main coursework and projects. Some leading examples include:
In this tutorial, we learned about the list data type with a special focus on how to find its size. This information on measuring the length of the list in Python is one of the most basic operations you would do as a beginner. So, use the above steps to level up our programming skills and stay curious for more!