1

Currently i have this

def convert_tuple(self, listobj, fields=None):
    return [(obj.start, obj.end) for obj in listobj]

But i have hard coded the fields.

I want to have fields as another list like

def convert_tuple(self, listobj, fields=['start', 'end', 'user']):
    return [(obj.field) for obj in listobj for field in fields]

How can i implement that

Expected output

[('2am', '5am', 'john'), ('3am', '5am', 'john1'), ('3am', '5am', 'john2') ]

where 2am is start , 5am is end , john is username

1
  • 1
    Example input with desired output would be helpful. Commented May 30, 2015 at 4:22

1 Answer 1

2

You can leverage python builtin getattr along with nested list comprehension to achieve what you are envisaging.

def convert_tuple(self, listobj, fields=['start', 'end', 'user']):
    return [(getattr(obj, field) for  field in fields)
            for obj in listobj] 

It is worth noting that your comprehension is rather a cartesian product rather than a nested comprehension

Sign up to request clarification or add additional context in comments.

1 Comment

How can i display it properly currently i get [genexpres, genexpression] , i want to expand it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.