The len()
function in Python is a built-in function that returns the number of elements in an object. The object can be a list, string, tuple, set, or dictionary.
Syntax
len(object)
Parameters
object
– any object (such as a list, string, tuple, set, or dictionary).
Return Value
The len()
function returns an integer, which is the number of elements in the object.
Examples
Example 1: Getting the length of a list
list = [1, 2, 3, 4] result = len(list) print(result)
Output:
4
In this example, we have defined a list, list
, which contains the numbers 1, 2, 3, and 4. The len()
function is then used to get the length of the list, by passing the list as an argument to the function. The resulting value is 4, which is the number of elements in the list.
Example 2: Getting the length of a string
string = "Hello World" result = len(string) print(result)
Output:
11
In this example, we have defined a string, string
, which contains the words “Hello World”. The len()
function is then used to get the length of the string, by passing the string as an argument to the function. The resulting value is 11, which is the number of characters in the string.
Example 3: Getting the length of a dictionary
dictionary = {'key1': 1, 'key2': 2, 'key3': 3} result = len(dictionary) print(result)
Output:
3
In this example, we have defined a dictionary, dictionary
, which contains 3 key-value pairs. The len()
function is then used to get the length of the dictionary, by passing the dictionary as an argument to the function. The resulting value is 3, which is the number of key-value pairs in the dictionary.
Use Cases
The len()
function is commonly used to:
- Determine the number of elements in a list, string, tuple, set, or dictionary
- Control the iteration of a loop based on the number of elements in an object
- Verify that an object is not empty before performing an operation on it
For example, you might use the len()
function to make sure that a list contains at least one element before appending an item to it, like this:
list = [1, 2, 3] if len(list) > 0: list.append(4) print(list)
Output:
[1, 2, 3, 4]
In this example, the len()
function is used to check that the list is not empty, and if it’s not, then a new item is added to the list. This way, you can ensure that you are only adding elements to an object that actually exists.
Another use case for the len()
function is to control the iteration of a loop. For example, you might use the function to loop through a list and print each item, like this:
list = [1, 2, 3, 4] for i in range(len(list)): print(list[i])
Output:
1 2 3 4
In this example, the len()
function is used to get the length of the list, and then a for loop is used to iterate through the list and print each item. This is a common use case for the len()
function, especially when you need to perform an operation on each element in an object.
Overall, the len()
function is a simple yet powerful tool in Python for working with objects and determining their size. Whether you’re working with lists, strings, tuples, sets, or dictionaries, the len()
function can help you quickly and easily determine the number of elements in your object, allowing you to make informed decisions about how to manipulate and interact with that object in your code.