How to Get the Length of a List in Python

How to get the length of a list in Python

This tutorial will teach you how to find the length of a list (array) in Python using some code examples.

There are four data structures built into Python: tuples, sets, dictionaries, and lists. They are important for programmers because they allow for iteration over their contents. When we talk about data structures such as lists in Python, we are referring to a collection data type. Lists in Python can be ordered and changed. Also, unlike the sets collection type, a list allows for duplicated entries, or can even be empty. Like the other collection data types, a list in Python may be indexed, sliced, added to, multiplied, or be checked to see if it contains a particular item.

Lists in Python

If you are familiar with any other programming languages, you may be familiar with arrays. Lists in Python are rather similar and can be iterated over in a number of ways. A Python list is a form of Data Structures container, which means it holds multiple items of data simultaneously. Unlike a set, a list has a definite count. Lists in Python are described as mutable sequences, which means that unlike other built-in data types such as sets or tuples, a Python list can be altered after it has been created. This means that a list can be an extremely versatile way of dealing with data.

Whereas arrays in C must contain variables of the same data type, lists in Python can contain any combination. For example, a list in Python could comprise of 66, ‘cow’, 99.9, and ‘wat?’.

Creating a list in Python

To create a list in Python, you must enclose the entries in square brackets and separate each entry with a comma. The usual inverted commas or speech marks for string type entries are also required. Therefore, a new list could be created and displayed as follows:

ls_list = ['Linux', 'Screw','Rocks!',123]
print(ls_list)

Each item in a list is indexed when the list is created, with the first list item set to position 0, the second item set to 1, the third set to 2, and so forth. Therefore, to find out which item is at a given index, the command is:

ls_list = ['Linux', 'Screw','Rocks!',123]
print(ls_list[2])

This will return the item set at position 2 of the list, which for this example list is ‘Rocks!’. It’s possible to then add items or otherwise iterate over the list based on item positions.

The len() method

A common operation carried out on a list is finding its length. There is a method available that makes this a very simple proposition.

The len() function will return the length of a list as follows:

ls_list = ['Linux', 'Screw','Rocks!',123]
print(len(ls_list) )

For our sample list, the list length will be displayed as 4. Remember that although the first index is 0, it is still counted as part of the list’s length.

The len() function is simple and versatile and can be used on other data collections such as tuples, arrays, and dictionaries. As such, it’s the most popular approach. However, there is another way to get the length of a list in Python, which is the Naïve method.

The Naive Method

This method is performed by iterating over the list. A loop is created and a counter incremented until the program completes its iteration over the entire list.

Although this may sound like a complex method, it again is very simple to code. It can be applied to our sample list as shown:

ls_list = ['Linux', 'Screw','Rocks!',123]
print('The list is: ' + str(ls_list))
counter = 0
for i in ls_list:
counter = counter + 1
print ('The length of the list is ' + str(counter))

This Naive method will then return 4, exactly as the len() function did.

Lists in Python are versatile and therefore very useful. Both the built-in len() function and the Naïve method mean it is extremely simple to establish the length of a list. Be sure to check out other articles if you need to learn how to reverse a list, remove items from a list, or find in a list in Python.

SHARE:
nv-author-image

Stefan Durand

My name is Stefan, I'm the admin of LinuxScrew. I am a full-time Linux/Unix sysadmin, a hobby Python programmer, and a part-time blogger. I post useful guides, tips, and tutorials on common Linux and Programming issues. Feel free to reach out in the comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *