Get The Number Of Items Of A List In Python - Nkmk Note
Maybe your like
In Python, you can get the size (length, number of items) of a list using the built-in len() function.
- Built-in Functions - len() — Python 3.11.3 documentation
- Get the size of a list with len()
- Get the size of a list of lists (2D list)
- Get the size of a list with lists and non-list items
You can get the total number of items using len(). If you want to count the number of occurrences of an item, use the count() method or the Counter class of the collections module.
- Count elements in a list with collections.Counter in Python
See the following article for how to use len() with objects of other types.
- How to use len() in Python
Get the size of a list with len()
Passing a list as an argument to the built-in len() function returns its size (length, number of items) as an integer value.
l = [0, 1, 2, 3] print(len(l)) # 4 source: list_len.pyGet the size of a list of lists (2D list)
If a list of lists (2D list) is passed directly to len(), the number of lists stored as elements is returned.
l_2d = [[0, 1, 2], [3, 4, 5]] print(len(l_2d)) # 2 source: list_len.pyThe number of items in each list (the number of items in each row) can be obtained using list comprehensions.
- List comprehensions in Python
The total number of items can be calculated with sum().
Generator expressions are used here, which are similar to list comprehensions but enclosed in (). In this example, the outer pair of parentheses can be omitted since the expression is already within another pair.
print(sum(len(v) for v in l_2d)) # 6 source: list_len.pyIf NumPy is installed in your environment, you can convert a list of lists to a NumPy array (numpy.ndarray).
You can get the total number of items with the size attribute, and the shape (number of rows and columns) with the shape attribute.
- Convert numpy.ndarray and list to each other
- NumPy: Get the number of dimensions, shape, and size of ndarray
Get the size of a list with lists and non-list items
Consider a list with lists and non-list items.
l_multi = [[0, 1, 2, [10, 20, 30]], [3, 4, 5], 100] source: list_len.pySee the following article on how to flatten such a list to one dimension.
- How to flatten a list of lists in Python
If this list is passed to len(), the number of objects stored as elements is returned.
print(len(l_multi)) # 3 source: list_len.pyPassing this list to numpy.array() causes an error (as of version 1.25.1). Older versions may generate a NumPy array (numpy.ndarray) with list elements.
# arr_multi = np.array(l_multi) # ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. # The detected shape was (3,) + inhomogeneous part. source: list_len.pyTo get the total number of elements in a list with lists and non-list items, you can define a function that recursively calculates the number of elements in the list.
def my_len(l): count = 0 if isinstance(l, list): for v in l: count += my_len(v) return count else: return 1 l_multi = [[0, 1, 2, [10, 20, 30]], [3, 4, 5], 100] print(my_len(l_multi)) # 10 l_2d = [[0, 1, 2], [3, 4, 5]] print(my_len(l_2d)) # 6 l = [0, 1, 2, 3] print(my_len(l)) # 4 source: list_len.pyThe isinstance() function is used to determine if an object is a list.
- Get and check the type of an object in Python: type(), isinstance()
Note that passing an object other than a list will return 1.
print(my_len(100)) # 1 source: list_len.pyFor example, to return None for an object that is not a list, you can define a wrapper function as follows.
def my_len_wrapper(l): if isinstance(l, list): return my_len(l) else: return None print(my_len_wrapper(l_multi)) # 10 print(my_len_wrapper(l_2d)) # 6 print(my_len_wrapper(l)) # 4 print(my_len_wrapper(100)) # None source: list_len.pyTag » How To Get The List Number
-
How Do I Get The Number Of Elements In A List In Python?
-
Python: Get Number Of Elements In A List - Stack Abuse
-
How To Get The Number Of Elements In A Python List? - GeeksforGeeks
-
Python List Count() - Programiz
-
Python List Index() - Programiz
-
Create A Bulleted Or Numbered List - Microsoft Support
-
Python : Get Number Of Elements In A List, Lists Of Lists Or Nested List
-
Python Count Number Of Elements In List - Full Guide - Stack Vidhya
-
How To Find The Number Of Elements In A List - Python - YouTube
-
Python Find String In List - DigitalOcean
-
How To Convert List To String In Python? Here Are The 6 Ways To Know
-
Python List Count() With EXAMPLES - Guru99
-
Python Unique List – How To Get All The Unique Values In A List Or ...
-
Python List Max() Method - Tutorialspoint