apply group by with list comprehension
form generation code reorg