In this tutorial, you will learn how to use the Python array len() method with program examples. Python len() is a built-in method used to return the number of elements in an array or object.
This method is useful for counting characters in a string, numbers, and even null values. As you go deeper into this tutorial, you can learn more about the method of len() and how it works in different types of Python objects.
What is Len in Python?
len() is one of the Python functions used to return the number of items for characters in an object.
Specifically, the Python array/object types include:
- list
- numbers
- boolean
- null value
- strings
Know more about the len() method by its syntax, return value, and parameters. Then we will have various examples in different Python objects.
Syntax
len(object)
Also read: What Is String In Python?
Return Value
The len() method returns the value of elements in objects and the case of strings, characters.
Parameters
The Python len() method can count the number of elements in each of the given object types. See the following example to understand how the len() method works.
Example 1:
# using len() in string
sample_string = "Python for Free"
print(len(sample_string))
Output:
15
For example 1, the len() method counts the elements of the sample_string “Python for Free”.
Remember that the Python len() also counts spaces as elements of the string. Therefore, there are fifteen (15) characters when you count the string elements from P to e.
Example 2:
# using len() in numbers
sample_numbers = (24,23,25,25,21)
print(len(sample_numbers))
Output:
5
The len() method works the same with numbers in Python. It returns the value of how many elements are in the sample_numbers. Regardless of the duplicate number, the method still counts it as different elements.
See more examples in the next topics.
How do you find the LEN of an array in Python?
To find LEN or the length of an array in Python, we use the len() method. The array has a specific value and len() is a Python method. Therefore, programmers use Python len() to return or count the number of elements in an array.
Let us look at the following examples:
Example 1:
sample_array = ("pythonforfree", [1, 2, 3])
print(len(sample_array))
Output:
2
From the given example, the function returns a value of one (1) since the array only contains a single element.
Now, take a look at another example.
Example 2:
sample_array = ("Prince", "Grace", "Jay", "Ben")
print(len(sample_array))
Output:
4
This example program counts the number of elements in an array and returns the value of six (6).
How about we use numbers in an array? Would it work similarly? For example,
Example 3
# using len() in array
sample_array = [1, 2, 1, 3, 4, 5, 3]
print(len(sample_array))
Output:
7
As seen in the example, the function works well in Python array. The output seven (7) is the count of the array elements, regardless of the value duplication.
Find the Length of a Python NumPy Array using len
In Numpy, there are two ways to find the length of an array:
- Using len() method
- Using .size method
To create a program that counts a length of an array in NumPy, you also need to include “import numpy“.
Explore the following examples and try them to confirm the outputs.
Example 1: Using len() function
import numpy
name = numpy.array(["Python", "for", "Free"])
print(len(name))
Output:
3
Example 2: Using .size function
import numpy
name = numpy.array(["Mary Grace","Prince Ly"])
print(name.size)
Output:
3
Either of the given examples will work just fine in your programs. The two function works similarly in NumPy programs.
Remember: Make sure you have the NumPy framework installed in your Python environment to avoid errors. If ever you need to install Numpy, refer to the complete direction of NumPy installation. It has several options for you to choose how would you install the NumPy framework.
Moving on, let us know how to…
Find the Length of a Python List using len
Going further into this discussion, let us now learn how Python len() works with lists.
The list is another type of Python object that also holds elements. len() method is also applicable in returning the value of elements included in a list. Take a look at the set of examples:
Example 1
sample_list = ["a", "b", "case", "daisy"]
print(len(sample_list))
Output:
4
Example 2
sample_list = [1, 22, 333]
print(len(sample_list))
Output:
3
Example 3
sample_list = ["a", "b", "c", "d", 1, 2, 3]
print(len(sample_list))
Output:
7
The above examples justify and explain how Python len() works with lists or arrays.
The tutorial gives a variety of examples to explain that whatever the kind of array is within the list, the Python len() method is still the same. Thus, any value of the array included in the list will be counted by the Python len().
Learn more about using Python list through the Easy Ways to learn Python Initialize List full tutorial.
Python Array Append
To append an array in Python, the append() function is used.
In Python, append() enables programmers to element into an array then you may confirm its addition with len().
Moreover, the append() is also a built- function of Python which is responsible for handling arrays as much as len(). Let’s take a look at the following examples to know how the append() function works in different types of arrays.
Example 1: Using append() function for lists
list1 = ["a", "b", "c", "d"]
list2 = [1, 2, 3]
list1.append(list2)
print(list1)
Output:
['a', 'b', 'c', 'd', [1, 2, 3]]
The example shows how the append() function adds the list2 at the end of list1. It explains that the function only applies according to the declaration of the program. The list2 was not directly added to the content of the list1 but as another list under the list1.
This clarifies that if the program is set to append list1 to list2, the output will be:
[1, 2, 3, ['a', 'b', 'c', 'd']]
This explains that the output depends on the exact declaration of the program function.
Example 2: Using append() function in NumPy Array
Prince Ly Y. Cesar
import numpy
sample1 = numpy.array([1,2,3,4])
sample2 = numpy.array([5,6,7,8])
solution1 = numpy.append(sample1,sample2)
print(solution1)
Output:
1 2 3 4 5 6 7 8
Now example 2 shows a different output from example 1. The function appends two arrays in Numpy and they were merged into a single list.
Python Array Index
Python index() or indexing is used to call out a value through the array indexes. The index is a specific value of an element or character in an array. So, to get a specific value with the array, this index() function is used.
Summary
Wrapping up, this tutorial completes the Python array len() context with examples.
The very purpose of Python len() is to know the length not just for arrays in Python but also in NumPy. Its examples were all explained and used to emphasize how the len() method works in different types of arrays.
Additionally, this tutorial also provides explanations for other Python built-in functions that place emphasis on the len() method for arrays. These built-in functions include the Python array index() and append.
Though these functions vary in method, they stick in handling Python and NumPy arrays.