How to get the size of a list in Python?

Small tutorial with code example and video on how to see the size of the list in Python
05 September 2017   2853

To get the size of a list, use the builtin function, len:

items = []
items.append("green")
items.append("orange")
items.append("yellow")

And type:

len(items)

len(s) argument returns the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).

len is implemented with __len__, from the data model docs:

object.__len__(self)

Called to implement the built-in function len(). Should return the length of the object, an integer >= 0. Also, an object that doesn’t define a __nonzero__() [in Python 2 or __bool__()in Python 3] method and whose __len__() method returns zero is considered to be false in a Boolean context.

And we can also see that __len__ is a method of lists:

items.__len__()

returns 3.

And in fact we see we can get this information for all of the described types:

>>> all(hasattr(cls, '__len__') for cls in (str, bytes, tuple, list, 
                                            xrange, dict, set, frozenset))
True

Python News Digest 18 - 24.01

Using SciPy for optimization, coders thoughts on safety in Python, hunter update and other interesting posts, articles and stuff
24 January 2020   133

Greetings! I hope your week went great! Here's new Python news digest.

Make some fun with Python, learn about Gandiva, how to filter pandas dataframe using the Multiple conditions and other interesting stuff

Guides

  • Scientific Python: Using SciPy for Optimization

Learn about SciPy ecosystem and how it differs from the SciPy library, how to install SciPy using Anaconda or pip and see some of its modules and check an examples that use the clustering and optimization functionality in SciPy

  • open and Python

Did you know that misusing Python’s open() and the interaction of CPython’s GC and UNIX semantics can lead to unexpected results

  • Having some fun with Python

Writing some strange code for fun and learning experiences

  • Gandiva, using LLVM and Arrow to JIT and evaluate Pandas expressions

This post talks about Gandiva - it provides a compiler to generate LLVM IR that can operate on batches of Apache Arrow

  • Pandas dataframe filter with Multiple conditions

Learn how to filter pandas dataframe using the Multiple conditions

Articles

  • Is there anything safe in python?

Skilled programmer's thoughts on safety in Python

Updates

  • voice flight following

This interesting solution reads the name closest city to the aircraft based on simulator GPS position

  • typer

Python lib to build CLI applications

  • sql2json

Python written tool that query a sql database and write an output in JSON format in standard output

  • Hunter

Code tracing toolkit