Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the time complexity of Python list's count() function?

I'm trying to figure what the time complexity of the count() function.

Ex if there is a list of [1, 2, 2, 3] and [1, 2, 2, 3].count(2) is used.

I've searched endlessly and looked at the Python wiki here, but its not there.

The closest I've come to finding an answer is here, but the field for complexity happens to be empty... Does anyone what the answer is?

like image 824
SW Williams Avatar asked Jun 28 '17 21:06

SW Williams


People also ask

What is the time complexity of count function in Python?

count() is O(n).

What is the time complexity of count () method?

Time Complexity: Time Complexity for unordered_set::count() method is O(1) in average cases, but in worst case, time complexity can be O(N) i.e. Linear, where N is size of container.

What is runtime complexity of the list's built in append () method?

Time Complexity: Append has constant time complexity i.e.,O(1). Extend has a time complexity of O(k). Where k is the length of the list which need to be added.

What is count in list Python?

Definition and Usage The count() method returns the number of elements with the specified value.


3 Answers

Dig into the CPython source code and visit Objects/listobject.c, you will find the source code for the count() method in there. It looks like this:

static PyObject *
list_count(PyListObject *self, PyObject *value)
{
    Py_ssize_t count = 0;
    Py_ssize_t i;

    for (i = 0; i < Py_SIZE(self); i++) {
        int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
        if (cmp > 0)
            count++;
        else if (cmp < 0)
            return NULL;
    }
    return PyLong_FromSsize_t(count);
}

What it does is to simply iterate over every PyObject in the list, and if they are equal in rich comparision (see PEP 207), a counter is incremented. The function simply returns this counter.

In the end, the time complexity of list_count is O(n). Just make sure that your objects don't have __eq__ functions with large time complexities and you'll be safe.

like image 98
Berk Özbalcı Avatar answered Oct 18 '22 10:10

Berk Özbalcı


Because the count method has to check every entry in the list, the runtime is going to be O(n).

like image 39
g.d.d.c Avatar answered Oct 18 '22 08:10

g.d.d.c


It needs needs to visit all elements in order to know whether to count them or not. There is no reason for it to do any more work than that.

So, it cannot possibly be better than O(n), and since even the most basic, simple, straightforward implementation is O(n), you would need to actually be either very stupid or very malicious to make it any slower.

Ergo, by common sense, the worst-case step complexity is most likely O(n).

like image 22
Jörg W Mittag Avatar answered Oct 18 '22 10:10

Jörg W Mittag