combine a lists of nested with dictionaries

Question:

I am trying to combine a list called Customer Details

  • [{‘First Name’: ‘Kieran’, ‘Last Name’: ‘Wilson’, ‘Age (Years)’: ’89’, ‘Marital Status’: ‘married or civil partner’, ‘Distance Commuted to Work (miles)’: ‘0’, ‘Employer Company’: ‘N/A’, ‘Dependants’: ‘3’, ‘Yearly Pension (GBP)’: ‘7257’, ‘Sex’: ‘Male’, ‘Retired’: ‘TRUE’, ‘Yearly Salary (GBP)’: ‘72838’}
    with a list called Address
  • [{‘Address Street’: ’70 Lydia isle’, ‘Address City’: ‘Lake Conor’, ‘Address Postcode’: ‘S71 7XZ’}, {‘Address Street’: ’00 Wheeler wells’, ‘Address City’: ‘Chapmanton’, ‘Address Postcode’: ‘L2 7BT’}
    and Credit card info
  • [{‘Credit Card Start Date’: ’18-Aug’, ‘Credit Card Expiry Date’: ’27-Nov’, ‘Credit Card CVV’: ‘875’, ‘Credit Card Number’: ‘6.76374E+11’, ‘Bank IBAN’: ‘GB62PQKB71416034141571’}
    and Vehicle
  • [{‘Vehicle Make’: ‘Hyundai’, ‘Vehicle Model’: ‘Bonneville’, ‘Vehicle Type’: ‘Pickup’, ‘Vehicle Year’: ‘2009’}

How do I combine this using First Name and Last Name in the Customer Details list

This is the result I am hoping to get

  • [
    {
    "First Name": "Kieran",
    "Last Name": "Wilson",
    "Age (Years)": "89",
    "Marital Status": "married or civil partner",
    "Distance Commuted to Work (miles)": "0",
    "Employer Company": "N/A",
    "Dependants": "3",
    "Yearly Pension (GBP)": "7257",
    "Sex": "Male",
    "Retired": "TRUE",
    "Yearly Salary (GBP)": "72838"},
    {
    "Address Street": "70 Lydia isle",
    "Address City": "Lake Conor",
    "Address Postcode": "S71 7XZ"
    },
    {
    "Credit Card Start Date": "18-Aug",
    "Credit Card Expiry Date": "27-Nov",
    "Credit Card CVV": "875",
    "Credit Card Number": "6.76374E+11",
    "Bank IBAN": "GB62PQKB71416034141571"
    },
    {
    "Vehicle Make": "Hyundai",
    "Vehicle Model": "Bonneville",
    "Vehicle Type": "Pickup",
    "Vehicle Year": "2009"
    }
    ]
    for the first customer
Asked By: Romoke

||

Answers:

customer_details = [{'First Name': 'Kieran', 'Last Name': 'Wilson', 'Age (Years)': '89', 'Marital Status': 'married or civil partner', 'Distance Commuted to Work (miles)': '0', 'Employer Company': 'N/A', 'Dependants': '3', 'Yearly Pension (GBP)': '7257', 'Sex': 'Male', 'Retired': 'TRUE', 'Yearly Salary (GBP)': '72838'}]
address = [{'Address Street': '70 Lydia isle', 'Address City': 'Lake Conor', 'Address Postcode': 'S71 7XZ'}, {'Address Street': '00 Wheeler wells', 'Address City': 'Chapmanton', 'Address Postcode': 'L2 7BT'}]
cc_info = [{'Credit Card Start Date': '18-Aug', 'Credit Card Expiry Date': '27-Nov', 'Credit Card CVV': '875', 'Credit Card Number': '6.76374E+11', 'Bank IBAN': 'GB62PQKB71416034141571'}]
vehicle = [{'Vehicle Make': 'Hyundai', 'Vehicle Model': 'Bonneville', 'Vehicle Type': 'Pickup', 'Vehicle Year': '2009'}]

result = [[customer_details[i], address[i], cc_info[i], vehicle[i]] for i in range(len(customer_details))]

print(result)

results in:

[[{'First Name': 'Kieran', 'Last Name': 'Wilson', 'Age (Years)': '89', 'Marital Status': 'married or civil partner', 'Distance Commuted to Work (miles)': '0', 'Employer Company': 'N/A', 'Dependants': '3', 'Yearly Pension (GBP)': '7257', 'Sex': 'Male', 'Retired': 'TRUE', 'Yearly Salary (GBP)': '72838'}, {'Address Street': '70 Lydia isle', 'Address City': 'Lake Conor', 'Address Postcode': 'S71 7XZ'}, {'Credit Card Start Date': '18-Aug', 'Credit Card Expiry Date': '27-Nov', 'Credit Card CVV': '875', 'Credit Card Number': '6.76374E+11', 'Bank IBAN': 'GB62PQKB71416034141571'}, {'Vehicle Make': 'Hyundai', 'Vehicle Model': 'Bonneville', 'Vehicle Type': 'Pickup', 'Vehicle Year': '2009'}]]
Answered By: EvilSmurf

You can use dictionary merging to achieve this.
Dictionary merge operator ** is new since Python 3.9. Be aware there are two different merge-operators, | and **. The |operator creates unions, which eliminates duplicate keys.

customer_details = [{'First Name': 'Kieran', 'Last Name': 'Wilson', 'Age (Years)': '89', 'Marital Status': 'married or civil partner', 'Distance Commuted to Work (miles)': '0', 'Employer Company': 'N/A', 'Dependants': '3', 'Yearly Pension (GBP)': '7257', 'Sex': 'Male', 'Retired': 'TRUE', 'Yearly Salary (GBP)': '72838'}]
address = [{'Address Street': '70 Lydia isle', 'Address City': 'Lake Conor', 'Address Postcode': 'S71 7XZ'}, {'Address Street': '00 Wheeler wells', 'Address City': 'Chapmanton', 'Address Postcode': 'L2 7BT'}]
credit_card_info = [{'Credit Card Start Date': '18-Aug', 'Credit Card Expiry Date': '27-Nov', 'Credit Card CVV': '875', 'Credit Card Number': '6.76374E+11', 'Bank IBAN': 'GB62PQKB71416034141571'}]
vehicle = [{'Vehicle Make': 'Hyundai', 'Vehicle Model': 'Bonneville', 'Vehicle Type': 'Pickup', 'Vehicle Year': '2009'}]

# Merge all the lists based on the customer's first name and last name
result = [{**customer, **{'Address': address, 'Credit Card Info': credit_card_info, 'Vehicle': vehicle}}           for customer in customer_details]

print(result)
Answered By: maddes8cht
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.