Home > front end >  Combining three different list collection of dictionary having same value in key name “firstname” an
Combining three different list collection of dictionary having same value in key name “firstname” an

Time:12-16

I have three different list collection of dictionary as shown all three have same "firstname" and lastname". I need to combine this list in a copy of one without replicating the firstname and lastname, ie for each firstname and lastname a combination of the other three list collection of dictionary:

list one

[{'First Name': 'Justin',
  'lastName': 'Walker',
  'Age (Years)': '29',
  'Sex': 'Male',
  'Vehicle Make': 'Toyota',
  'Vehicle Model': 'Continental',
  'Vehicle Year': '2012',
  'Vehicle Type': 'Sedan'},
 {'First Name': 'Maria',
  'lastName': 'Jones',
  'Age (Years)': '66',
  'Sex': 'Female',
  'Vehicle Make': 'Mitsubishi',
  'Vehicle Model': 'Yukon XL 2500',
  'Vehicle Year': '2014',
  'Vehicle Type': 'Van/Minivan'},
 {'First Name': 'Samantha',
  'lastName': 'Norman',
  'Age (Years)': '19',
  'Sex': 'Female',
  'Vehicle Make': 'Aston Martin',
  'Vehicle Model': 'Silverado 3500 HD Regular Cab',
  'Vehicle Year': '1995',
  'Vehicle Type': 'SUV'}

list two

[{'firstName': 'Justin',
  'lastName': 'Walker',
  'age': 71,
  'iban': 'GB43YKET96816855547287',
  'credit_card_number': '2221597849919620',
  'credit_card_security_code': '646',
  'credit_card_start_date': '03/18',
  'credit_card_end_date': '06/26',
  'address_main': '462 Marilyn radial',
  'address_city': 'Lynneton',
  'address_postcode': 'W4 0GW'},
 {'firstName': 'Maria',
  'lastName': 'Jones',
  'age': 91,
  'iban': 'GB53QKRK45175204753504',
  'credit_card_number': '4050437758955103343',
  'credit_card_security_code': '827',
  'credit_card_start_date': '11/21',
  'credit_card_end_date': '01/27',
  'address_main': '366 Brenda radial',
  'address_city': 'Ritafurt',
  'address_postcode': 'NE85 1RG'}]

list three

{'firstName': 'Justin',
  'lastName': 'Walker',
  'age': '64',
  'sex': 'Male',
  'retired': 'False',
  'dependants': '2',
  'marital_status': 'single',
  'salary': '56185',
  'pension': '0',
  'company': 'Hudson PLC',
  'commute_distance': '14.1',
  'address_postcode': 'G2J 0FH'},
 {'firstName': 'Maria',
  'lastName': 'Jones',
  'age': '69',
  'sex': 'Female',
  'retired': 'False',
  'dependants': '1',
  'marital_status': 'divorced',
  'salary': '36872',
  'pension': '0',
  'company': 'Wall, Reed and Whitehouse',
  'commute_distance': '10.47',
  'address_postcode': 'TD95 7FL'}

This is what I trying but

for i in range(0,2):
    dict1 = list_one[i]
    dict2 = list_two[i]
    dict3 = list_three[i]
    combine_file = list_three.copy()
    for k, v in dict1.items():
        if k == "firstname" or "lastname":
            for k1, v1 in combine_file.items():
                if dict1.get(k) == combine_file.v1:                
            

This is what I'm expecting print(combine_file)

{'firstName': 'Justin',
  'lastName': 'Walker',
  'age': '64',
  'sex': 'Male',
  'retired': 'False',
  'dependants': '2',
  'marital_status': 'single',
  'salary': '56185',
  'pension': '0',
  'company': 'Hudson PLC',
  'commute_distance': '14.1',
  'iban': 'GB43YKET96816855547287',
  'credit_card_number': '2221597849919620',
  'credit_card_security_code': '646',
  'credit_card_start_date': '03/18',
  'credit_card_end_date': '06/26',
  'address_main': '462 Marilyn radial',
  'address_city': 'Lynneton',
  'address_postcode': 'W4 0GW',
  'Vehicle Make': 'Mitsubishi',
  'Vehicle Model': 'Yukon XL 2500',
  'Vehicle Year': '2014',
  'Vehicle Type': 'Van/Minivan'},
 {'firstName': 'Maria',
  'lastName': 'Jones',
  'age': '69',
  'sex': 'Female',
  'retired': 'False',
  'dependants': '1',
  'marital_status': 'divorced',
  'salary': '36872',
  'pension': '0',
  'company': 'Wall, Reed and Whitehouse',
  'commute_distance': '10.47',
  'iban': 'GB53QKRK45175204753504',
  'credit_card_number': '4050437758955103343',
  'credit_card_security_code': '827',
  'credit_card_start_date': '11/21',
  'credit_card_end_date': '01/27',
  'address_main': '366 Brenda radial',
  'address_city': 'Ritafurt',
  'address_postcode': 'NE85 1RG',
  'Vehicle Make': 'Aston Martin',
  'Vehicle Model': 'Silverado 3500 HD Regular Cab',
  'Vehicle Year': '1995',
  'Vehicle Type': 'SUV'}

CodePudding user response:

Create a new dictionary keyed on a composite of either 'firstname_lastname' or 'First Name_lastname' then you can do this:

master = {}

for _list in list_1, list_2, list_3:
    for d in _list:
        if not (firstname := d.get('firstName')):
            firstname = d['First Name']
        name_key = f'{firstname}_{d["lastName"]}'
        for k, v in d.items():
            master.setdefault(name_key, {})[k] = v

print(list(master.values()))

CodePudding user response:

Python's dict.update() functionality might be what you are looking for.

For example:

dict1 = { 'a' : 0, 'b' : 1, 'c' : 2}

dict2 = { 'c' : 0, 'd' : 1, 'e' : 2}

dict2.update(dict1)

dict2 is now: {'a' : 0, 'b' : 1, 'c' : 2, 'd' : 1, 'e' : 2}

Notice how 'c' was overwritten with the updated value from dict1.

You can't update together dictionaries from different people, but if you run through your lists before hand you could compile sets of dictionaries where each set belongs to one person.

You can create a new dictionary, called people, and then iterate through your lists of dictionaries and extract the person's name from those dictionaries and turn it into a key in the new "people" dictionary.

If that person's name is not in people yet, you can add that dictionary, so that people[name] points to that dictionary.

If people[name] does exist, then you can use the people[name].update() function on the new dictionary to add the new values.

After this process you will have a dictionary whose keys are the names of people and the values point to a dictionary containing those people's attributes.

  • Related