HS

harsh_shukla

Intern at Microsoft July 30, 2020, 10:21 a.m. ⋅ 155 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

 



Go back to feed

HackersFriend Updates





View more


Events


Nov. 28, 2018, 5:30 p.m.

Python from zero to hero

place Delhi

View details


Aug. 13, 2018, 5:30 p.m.

Python from zero to hero

place Bangalore ( HackersFriend office BTM Layout)

View details