Which method call returns the number of elements in my_list?

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.

len(s)

Return 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.

2 List in Python

Which method call returns the number of elements in my_list?

Python includes several built-in container types, but the most common ones are lists and tuples, which we would see in this tutorial.

There are certain things you can do with all container types. These operations include indexing, slicing, adding, multiplying. In addition, Python has built-in functions like finding the length of a sequence, finding its largest and smallest elements…

Each element of a sequence is assigned by a number - its position or index. The first index is zero, the second index is one…

Creating a list is as simple as putting different comma-separated values between square brackets :

  • list1 = [ a, b, c, d, e]
  • list2 = [ 1, 2, 3, 4, 5]

Python is an object-oriented language, lists are associated with methods: object.method() Functions can be applied to lists.

(1) Count the Number of Elements in a Python List that Contains Strings

To start with a simple example, let’s create a list that contains 5 names:

names_list = ['Jeff', 'Ben', 'Maria', 'Sophia', 'Rob'] print(names_list)

Run the syntax above, and you’ll get the following list:

['Jeff', 'Ben', 'Maria', 'Sophia', 'Rob']

You can then use the len() function in order to count the number of elements in the list:

names_list = ['Jeff', 'Ben', 'Maria', 'Sophia', 'Rob'] print(len(names_list))

Once you run the code in Python, you’ll get the count of 5.

Let’s extend the list by additional 3 names, and then recount the number of elements:

names_list = ['Jeff', 'Ben', 'Maria', 'Sophia', 'Rob'] names_list.extend(['Laura','Elizabeth','Justin']) print(len(names_list))

You’ll now get the count of 8.

Python List

In this tutorial, we'll learn everything about Python lists: creating lists, changing list elements, removing elements, and other list operations with the help of examples.

11. Lists¶

A list is an ordered collection of values. The values that make up a list are called its elements, or its items. We will use the term element or item to mean the same thing. Lists are similar to strings, which are ordered collections of characters, except that the elements of a list can be of any type. Lists and strings — and other collections that maintain the order of their items — are called sequences.