Python implementation of a singly linked list This is my code for the implementation of a singly linked list in Python:

class SList:
    def __init__(self):
        self.root = None
        self.size = 0

    def insert(self, item):
        try:
            if not item:
                raise ValueError
            self.size += 1
            if self.root is None:
                self.root = Node(item)
            else:
                p = Node(item)
                p.next = self.root
                self.root = p
        except ValueError:
            raise ValueError('Cannot add None item to a list')

    def remove(self, index):
        try:
            if index &lt; 0 or index &gt;= self.size:
                raise ValueError

            current = self.root
            count = 0
            while count &lt; index:
                current = current.next
                count += 1
            current.next = current.next.next
            self.size -= 1
        except ValueError:
            raise ValueError('Index cannot be negative or greater than the size of the list')

    def __sizeof__(self):
        return self.size


class Node:
    def __init__(self, data):
        try:
            if not data:
                raise ValueError
            self.data = data
            self.next = None
        except ValueError:
            raise ValueError('Node cannot be instantiated without an item')
raise + except + raise = raise

def remove(self, index):
    try:
        if index &lt; 0 or index &gt;= self.size:
            raise ValueError

            ...

    except ValueError:
        raise ValueError('Index cannot be negative or greater than the size of the list')


It make no sense to except and raise again, simplify to:

if index &lt; 0 or index &gt;= self.size:
    raise IndexError('Index cannot be negative or greater than the size of the list')

...


The function will look like:

def insert(self, item):
    if item is None:
        raise ValueError('Cannot add None item to a list')
    self.size += 1
    if self.root is None:
        self.root = Node(item)
    else:
        p = Node(item)
        p.next = self.root
        self.root = p


for instead of counting loop

for is a more streamlined way of looping n times than a while loop:

        count = 0
        while count &lt; index:
            current = current.next
            count += 1


Becomes:

for _ in range(index):
    current = current.next


(Please note _ is a convention meaning the value will not be used)