fork download
  1. class Customer:
  2. def __init__(self, cust_id, cust_name, location):
  3. self.cust_id = cust_id
  4. self.cust_name = cust_name
  5. self.location = location
  6.  
  7. list_of_customers = [Customer(101, 'Mark', 'US'),
  8. Customer(102, 'Jane', 'Japan'),
  9. Customer(103, 'Kumar', 'India')]
  10.  
  11. dict_of_customer = {}
  12. for customer in list_of_customers:
  13. dict_of_customer[customer.location] = customer
  14.  
  15. print ("Customer name in India is "+dict_of_customer["India"].cust_name)
  16.  
  17. for key,value in dict_of_customer.items():
  18. print ("Location: "+key+", Name: "+value.cust_name+", Id: "+(str(value.cust_id)))
Success #stdin #stdout 0.03s 9640KB
stdin
Standard input is empty
stdout
Customer name in India is Kumar
Location: US, Name: Mark, Id: 101
Location: Japan, Name: Jane, Id: 102
Location: India, Name: Kumar, Id: 103