qb.hostinfo() is what you want, and is not a terribly expensive operation even when you have several hundred hosts. (Unlike qb.jobinfo(subjobs=True,agenda=True), which can be very expensive when you have 1,000's of jobs in the system. Calling qb.jobinfo() with no filters is to be avoided if at all possible)
That being said, list comprehensions can make things a bit easier:
groups = [x['groups'] for x in qb.hostinfo() if len(x['groups'])]
which will get you something like:
['testGrpA,testGrpB', 'sample2']
Since hosts can belong to more than 1 group, the group membership for any 1 host can be a comma-separated string, like 'testGrpA,testGrpB' in the example above, so you still have to iterate over the list of groups, splitting then and adding them into a set or some other way of culling duplicates.