Tutorials  Articles  Notifications  Login  Signup


HS

Harsh Shukla

Intern at Microsoft July 30, 2020, 10:21 a.m. ⋅ 978 views

Python get number of items in a list


To get number of items in a list you can use len() function of python. len() function can be used with several different types in Python - both built-in types and library types.

Have a look.

>>> len([1,2,3])
3

Important thing to note is, to not use len() function to check emptiness of any list, however, you should use it to test length.

To check required length

if len(items) == required_length:
    ...

To check emptiness or presence of value

if items:
    ....
    # This block will be executed if items list is not empty
if not items:
    ...
    # This block will be executed if items list is empty

 



HackerFriend Logo

Join the community of 1 Lakh+ Developers

Create a free account and get access to tutorials, jobs, hackathons, developer events and neatly written articles.


Create a free account