items = []
items.append("apple")
items.append("orange")
items.append("banana")
# FAKE METHOD::
items.amount() # Should return 3
How do I get the number of elements in the list?
items = []
items.append("apple")
items.append("orange")
items.append("banana")
# FAKE METHOD::
items.amount() # Should return 3
How do I get the number of elements in the list?
Answering your question as the examples also given previously:
The
len()
function can be used with a lot of types in Python - both built-in types and library types.To find the size of a list, use the builtin function,
len
:And now:
returns 3.
Explanation
Everything in Python is an object, including lists. All objects have a header of some sort in the C implementation.
Lists and other similar builtin objects with a "size" in Python, in particular, have an attribute called
ob_size
, where the number of elements in the object is cached. So checking the number of objects in a list is very fast.But if you're checking if list size is zero or not, don't use
len
- instead, put the list in a boolean context - it treated as False if empty, True otherwise.From the docs
len(s)
len
is implemented with__len__
, from the data model docs:object.__len__(self)
And we can also see that
__len__
is a method of lists:returns 3.
Builtin types you can get the
len
(length) ofAnd in fact we see we can get this information for all of the described types:
Do not use
len
to test for an empty or nonempty listTo test for a specific length, of course, simply test for equality:
But there's a special case for testing for a zero length list or the inverse. In that case, do not test for equality.
Also, do not do:
Instead, simply do:
or
I explain why here but in short,
if items
orif not items
is both more readable and more performant.Besides
len
you can also useoperator.length_hint
(requires python 3.4+). For normallist
both are equivalent butlength_hint
makes it possible to get the length of a list-iterator, which could be useful in certain circumstances:But
length_hint
is by definition only a "hint", so most of the timelen
is better.I've seen several answers suggesting accessing
__len__
. This is alright when dealing with built-in classes likelist
but it could lead to problems with custom classes becauselen
(andlength_hint
) implement some safety checks. For example both do not allow negative lengths or lengths that exceed a certain value (thesys.maxsize
value). So it's always safer to use thelen
function instead of the__len__
method!And for completeness (taking one for the team with the downvotes), it is possible without using the
len()
function (I would not condone this as a good option):(the colon in
list[:]
is implicit, therefore also optional)While this may not be useful due to the fact that it'd make a lot more sense as being "out of the box" functionality, a fairly simple hack would be to build a class with a
length
property:You can use it like so:
Essentially, it's exactly identical to a list object, with the added benefit of having an OOP-friendly
length
property.As always, your mileage may vary.