"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Find the Number of Elements in a List (List Length) in Python?

How to Find the Number of Elements in a List (List Length) in Python?

Published on 2024-11-09
Browse:446

How to Find the Number of Elements in a List (List Length) in Python?

Finding the Number of Elements in a List (List Length) in Python

In Python, determining the number of elements in a list, also known as the list length, is a common operation. To achieve this, we can utilize the len() function.

For instance, consider the list items = ["apple", "orange", "banana"]. To find the number of elements in this list:

items_length = len(items)
print(items_length)

This will print 3, which represents the count of elements in the list.

The len() function is not limited to lists. It can be used with various other types, including strings, tuples, sets, and dictionaries. For example:

>>>> len([1, 2, 3])  # List
3
>>>> len("Hello")  # String
5
>>>> len({1, 2, 3})  # Set
3
>>>> len({"apple": "fruit", "banana": "fruit"})  # Dictionary
2
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3