Getting the Length of a List in Python

To get the length of a list in Python, you can use the len(list) function. The len() function works similarly with other data structures, including tuples, sets, and dictionaries. The list's size is not fixed; the user can add or remove data to the list during execution time. The len() method always returns the actual size of the list. It does not calculate the size of the list every time it is used; instead, it uses a pre-computed and cached value for the list's size, which makes the len() method very fast. Click Execute to run the Python List Length Example online and see the result.
Getting the Length of a List in Python Execute
fruits = ['apple', 'pear', 'banana']
print(len(fruits))
Updated: Viewed: 3879 times

What is the list in Python?

A Python list is a data structure that contains an ordered sequence of elements. A list can contain elements of various types, including int, float, string, custom class, and even another list. In Python, lists are mutable, which means that a list can be modified by adding or removing elements or by sorting the list. The size of the list is not fixed and changes every time you add or remove items from the list. To get the length of a list in Python, you can use the len() method.

An example of getting the length of a Python list using len() function.

Python list length example
fruits = ['apple', 'pear', 'banana']
print(len(fruits))

# 3

How to create a list in Python?

To create a list in Python, you can use square brackets "[]". You can initialize a list with data at creation time by placing the data in parentheses and separating it with commas. The data in the list can be of any type. Data of different types can be stored in one list.

An example of creating a Python list with a length of three and initializing it with data.

Create and initialize Python list
my_list = [123, 'qwe', {'key': 'value'}]
print(my_list)

# [123, 'qwe', {'key': 'value'}]

As you can see from the above example, the list contains different types of data.

How to get the length of the list?

To get the length of a Python list, you can use the len() function.

Getting the length of a Python list
print(len(['apple', 'pear', 'banana']))
  
# 3

In this example, the length of the list is 3.

How to add an item to a Python list?

To add an item to the end of the list, use the append() method.

Adding an item to the end of the list
fruits = ['apple', 'pear', 'banana']
fruits.append('orange')
print(fruits)

# ['apple', 'pear', 'banana', 'orange']

You can also create a list by concatenating an existing list with a new one using the "+" operator.

Add an item to a Python list using the "+" operator
fruits = ['apple', 'pear', 'banana']
berries = ['strawberry', 'raspberries', 'blueberry']
fruits_and_berries = fruits + berries
print(fruits_and_berries, len(fruits_and_berries))

# (['apple', 'pear', 'banana', 'strawberry', 'raspberries', 'blueberry'], 6)

The new list will contain elements from both lists, and the length of the new list will be equal to the length of the first list plus the length of the second list.

How to remove items from a list?

To clear the list, use the clear() method. If you want to remove any item from the list, you can do it in the following ways:

Remove item from list using the pop() method
fruits = ['apple', 'pear', 'banana']
fruits.pop(1)
print(fruits)

# ['apple', 'banana']

Remove item from list using the remove() method
fruits = ['apple', 'pear', 'banana']
fruits.remove('banana')
print(fruits)

# ['apple', 'pear']

Remove item from list using the del method/span>
fruits = ['apple', 'pear', 'banana']   
del fruits[:1]
print(fruits)

# ['pear', 'banana']

How to get the length of an object in the list?

The len () method can work with most types in Python, not just lists. To get the length of an element in a list, you can refer to that element by index and then get its length using the len () method.

Getting the length of list objects
my_list = ['qwerty', (1, 2, 3)]
print(len(my_list[0]), len(my_list[1]))

# (6, 3)

Other ways to get the length of a list in Python

You can find out the length of the list without using the len() method by using a "for _ in my_list:" loop and manually counting the number of elements in the list, or using the length_hint() operator to calculate the total number of elements in the list.

The fastest way to get the length of a list in Python

The len() method does not calculate the size of the list every time it is called. Instead, it uses a precomputed and cached value for the size of the list. Hence, using the len() method is the fastest way to calculate the size of a list in Python.

See Also