python
Get the length of a list in Python
If you are working on lists in Python and want to get the length of the list, you can use the len() function that returns the length of the list.
# define a list
my_list = ['a', 'b', 'c', 'd']
# Get length of the list using len() method
result = len(my_list)
# Print the result
print(result)
Output
4
The len() function takes the list as a parameter and returns its length in integer format. In the above example, we are passing a list my_list inside the len() function. The list has four items in it so the function len(my_list) will return 4.
Syntax
len(List)
Was this helpful?
Similar Posts
- Get string length using len() method python
- The Maximum Length of a Python String Explained
- Create a flat list from list of lists in Python
- Check if a list exists in a list of lists Python
- Convert a List of Strings to List of Integers in Python
- Get all values by key name from a list of dictionaries Python
- Get column names from Pandas DataFrame as a python List