# Trouble doing Data Structures and Algorithms with Python

So Mosh said in his Data Structures and Algorithms video that you can take the lessons there and apply it to other coding languages and it be pretty straight forward, but right now I’m really having difficulty converting it over to python. Right now I’m at creating Linked Lists from scratch and I’m having trouble creating the ‘addLast()’ functions(method). I don’t know what I’m doing wrong here.

``````class LinkedList:
# Nested Node class
class Node:
def __init__(self, value, next):
self.value = value
self.next = next

# Linked List class/functions
def __init__(self):
self.first = None
self.last = None

self.value = item
``````

If I have understood correctly, the addLast method should be updating the first and last variables and creating new Node objects as needed. So it should look something like this:

``````class Node:
def __init__(self, value):
self.value = value
self.next = None

node = Node(value)
if not first:
first = node
last = node
last.next = node
last = node
``````

Thanks I actually figured it out now, converting the syntax to python makes it come out like this:

``````    def addLast(self, item):
node = Node(item)
if self.first == None:
self.first = node
self.last = node
else:
self.last.next = node
self.last = node
``````

Now I’m actually having trouble making the indexOf function because with my current code it gives me this message:

``````    def indexOf(self, item):
index = 0
current = self.first
while current != None:
if current.value == item:
return index
else:
current = current.next
index += 1
return 'That numbers not in list'

def contains(item):
return indexOf(item) != 'That numbers not in list'
``````

My current code keeps giving me this Type Error:

TypeError: LinkedList.contains() takes 1 positional argument but 2 were given

First argument for contains should be self.

I did change that, but it then gives me this error message:

``````    def contains(self, item):
return indexOf(item) != 'That numbers not in list'
``````

line 45, in contains
return indexOf(item) != ‘That numbers not in list’
TypeError: indexOf expected 2 arguments, got 1

I believe you need to call it as self.indexOf

1 Like

For the record this is a terrible practice: a method that returns a number in some cases and a string in another. You should have it either return a signal value (like -1) or throw an error. Returning -1 on this type of method for not found is fairly standard.

Thank you! That worked for me, I’m still new to classes/OOP and I didn’t realize that previous functions can be called in other functions as methods through ’ self.(Function/method) '. Also for raising an error, do you mean like a Value Error?

If you want to follow what list does, raise a ValueError.

Your code will probably be simpler though if you just return -1 for not found, but it does mean that callers need to know and handle what -1 means which makes the error route more appealing.

1 Like

Ok, thanks for the tip!