rockstiff
rockstiff

Reputation: 383

Passing dictionary as argument with different key name

Im relative new to python and i still have problems thinking the pythonic way. I have a funcion that has CustomerAdd(id_type, id, name, email, adress, phone_number) and want to pass the customer as argument: CustomerAdd(**customer).

The problem that i have is that the expected customer format is:

{
  "name": [
    "CustName"
  ],
  "address": [
    "CustAddress"
  ],
  "id_type": "passport",
  "id": "123123123",
  "email": "[email protected]",
  "phone_number": "123123"
}

And my customer input looks like this:

{
  "id_type": "passport",
  "id": "123123123",
  "customer_name": "Gordon Gekko",
  "customer_address": "Fake Street 123",
  "phone_number": "123456789",
  "email": "[email protected]"
}

So, as you can see, i need to change the key names of customer_name and customer_address and also change the type of the values to a list.

I was able to change the key names with:

customer = old_customer.copy()
customer['name'] = customer.pop('customer_name')
customer['address'] = customer.pop('customer_address')

But still cant change the value type. Any ideas? Or any other pythonic way to solve this?

Thanks

Upvotes: 0

Views: 44

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599778

If they need to be lists, just make them lists:

customer['name'] = [customer.pop('customer_name')]
customer['address'] = [customer.pop('customer_address')]

Upvotes: 3

Related Questions