Python: Get Number of Elements in a List

python_tutorials

Introduction

Getting the number of elements in a list in Python is a common operation. For example, you will need to know how many elements the list has whenever you iterate through it. Remember that lists can have a combination of integers, floats, strings, booleans, other lists, etc. as their elements:

# List of just integers
list_a = [12, 5, 91, 18]

# List of integers, floats, strings, booleans
list_b = [4, 1.2, "hello world", True]

If we count the elements in list_a we get 5 elements overall. If we do the same for list_b we will get 4 elements.

There are different ways to get the number of elements in a list. The approaches vary whether you want to count nested lists as one element or all the elements in the nested lists, or whether you’re only interested in unique elements, and similar.

Built-in

 

 

To finish reading, please visit source site