'AttributeError: 'list' object has no attribute 'first' how can i solve?

I am trying to measure time for sorting list A with different sorting algorithms, (insertion, merge, quick)

and the error says:

AttributeError: 'list' object has no attribute 'first'

error occurred in the 5th line

def quick_sort(S):
    n = len(S)
    if n < 2:
        return
    p = S.first()
    L = LinkedQueue()
    E = LInkedQueue()
    G = LinkedQueue()
    while not S.is_empty():
        if S.first() < p:
            L.enqueue(S.dequeue())
        elif p < S.first():
            G.enqueue(S.dequeue())
        else:
            E.enqueue(S.dequeue())
    quick_sort(L)
    quick_sort(G)
    while not L.is_empty():
        S.enqueue(L.dequeue())
    while not E.is_empty():
        S.enqueue(E.dequeue())
    while not G.is_empty():
        S.enqueue(G.dequeue())

n = 100

and 5th from here (array_quick)

array = [random.randint(0, 999999999) for _ in range(n)]

array_quick = array.copy()
start = time.perf_counter()
quick_sort(array_quick)
t_quick = time.perf_counter() - start

if not is_sorted(array_quick):
    print("quick_sort:     incorrect")
else:
    print("quick_sort running time:", t_quick)


Solution 1:[1]

you should use S[0] instead of S.first()

Solution 2:[2]

Are you defining a method for the first() function on your list?

I don't think that is an inherent method of lists.

This is what a naive list returned for me:

>>> a = []
>>> dir(a)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',
 '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', 
'__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__',
 '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', 
'__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', 
'__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', 
'__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend',
 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

Maybe your list object had this method defined in a different project?

class list(list):
    def __init__(self, *args):
        super().__init__(args)
    def first(self):
        return self[0]

a = list("item", "another", "info")

print(a.first())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hardik singh
Solution 2 ondas