Here’s one utility function that worked for me:
def printList(L):
# print list of objects using string representation
if (len(L) == 0):
print "[]"
return
s = "["
for i in range (len(L)-1):
s += str(L[i]) + ", "
s += str(L[i+1]) + "]"
print s
This works ok for list of any object, for example, if we have a point class as below:
class Point:
def __init__(self, x_crd, y_crd):
self.x = x_crd
self.y = y_crd
def __str__(self):
return "(" + str(self.x) + "," + str(self.y) + ")"
And have a list of Point objects:
L = []
for i in range(5):
L.append(Point(i,i))
printList(L)
And the output comes as:
[(0,0), (1,1), (2,2), (3,3), (4,4)]
And, it served my purpose.
Thanks.
4
solved Printing list shows single quote mark