M.Jones M.Jones - 7 months ago 13
Python Question

how to remove a comma from a list

I'm having difficulty trying to remove the commas in my list. I have tried using split(',') but then realised you can't because its all numbers. Any advice would be great !

class UnorderedList:

def __init__(self):
self.head = None


def add(self, item): #add to the beginning of the list
new_node = Node(item)
new_node.set_next(self.head)
self.head = new_node

def size(self):
current = self.head
count = 0
while current != None:
count = count + 1
current = current.get_next()
return count

def is_empty(self):
return self.head == None

def search(self,item):
current = self.head
while current != None:
if current.get_data() == item:
return True
else:
current = current.get_next()
return False

def remove(self, item):
#Assumes the item is in the linked list
current = self.head
previous = None
found = False
while not found:
if current.get_data() == item:
found = True
else:
previous = current
current = current.get_next()
if previous == None:
self.head = current.get_next() #remove the first node
else:
previous.set_next(current.get_next())

def __str__(self):
my_list1 = []
current = self.head
while current != None:
my_list1 += [current.get_data()]
current = current.get_next()
return str(my_list1)

def __iter__(self):
return LinkedListIterator(self.head)


My answer is coming out as [8, 7, 6, 3, 5]
But I want it is [8 7 6 3 5]

Answer

Instead of:

str(my_list1)

Do:

"[{0}]".format(" " .join(str(x) for x in my_list1))