New New - 1 year ago 86
Python Question

api.LookupUser([u.user_id for u in userlist])

With Python, I have a userlist with a bunch of userids [1234,321,1203,2348,45955] and so forth. I'm trying to pass the userids into the api.LookupUser function.

This function usually takes one user_id at a time. For example, api.LookupUser(user_id=123)

Is there an easy way to pass each value from the userlist into the function?

Answer Source

I think you are looking for this

users = (api.LookupUser(user_id=x) for x in userids) 

Or, at least in Python3,

users = map(api.LookupUser, userids) 

And these return a generator, so you'll call next(users) to get the next value. I believe this also delays the API call, so you aren't getting all the users at once and bogging down the API

So, looping though that generator

for u in users:
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download