I have a list of tuples that consist of objects of a certain class and differ in their dimension:
my_list = [(obj1, obj2), (obj3, obj1), (obj4, obj1), (obj2,),...]
All of these objects have an attribute label which is of type string and holds an object label e.g. 'object_1', 'object_2' and so on.
Now I want to extract the string representation of the tuples holding the attribute values (labels) e.g.:
my_new_list = ['(object_1, object_2)', '(object_3, object_1)', '(object_4, object_1)', '(object_2,)', ...]
I have looked up how to extract a list of attributes from a list of objects and how to convert a tuple to string and would probably achieve my goal with some nested for loops. But I got stuck in some messy code here..
But isn't there some nice pythonic way to do this?
Thanks in advance!
*EDIT: Some of the tuples are one dimensional e.g. (obj2,). Sorry, I forgot that and now have adapted my question!
for obj1, obj2 in my_listto bring out the tuples, and then append'(%s,%s)' % (obj1.label, obj2.label)to an initially empty list. And you can see that same structure in the answers below. Edit: Just saw the edit. Same general principle applies.__repr__function so they can be used in a manner like this.print(my_obj)is just nicer thanprint(my_obj.label)if the label makes sense to be the...well...label of the object.