home
Learn
screen_rotation
class A: name="Atul" city="Buxar" class B: name="Mohit" city="Delhi" class C: name="Rahul" city="Patna" userlist = [A(),B(),C()] newuserlist = [] for obj in userlist: user = {} user["name"] = obj.name user['city'] = obj.city newuserlist.append(user) print(newuserlist) # using comprehension newuserlist = [] newuserlist = [{"name":obj.name,"city":obj.city}for obj in userlist] print(newuserlist)
Program Output
[{'name': 'Atul', 'city': 'Buxar'}, {'name': 'Mohit', 'city': 'Delhi'}, {'name': 'Rahul', 'city': 'Patna'}] [{'name': 'Atul', 'city': 'Buxar'}, {'name': 'Mohit', 'city': 'Delhi'}, {'name': 'Rahul', 'city': 'Patna'}]