Home > Software design >  Combine multiple lists in one with index
Combine multiple lists in one with index

Time:09-17

I'm looking for solution to combine several lists into one that can be converted to a table later. All lists have same elements and some of them have uniq items.

person1 = [['Name','Alex'], ['Gender','M'], ['Age', 30]]
person2 = [['Name','Anna'], ['Gender','F'], ['Phone','1234567'],['Age', 25]]
person3 = [['Name','Fred'], ['Gender','M'], ['Age', 33], ['Passport', '987654']]

I would like to convert it to the list/table as follows:

# Name     Alex Anna    Fred 
# Gender   M    F       M
# Phone    #N/A 1234567 #N/A
# Age      30   25      33
# Passport #N/A #N/A    987654

I was trying to use dict, however cannot create a function to add data if key already exists:

combined = {}

for item in person1:
    if item[0] in combined:
        # How to add data, if key already exists in dict ?
        print('Exists')
    else:
        combined[item[0]] = item[1]

for item in person2:
    if item[0] in combined:
        # How to add data, if key already exists in dict ?
        print('Exists')
    else:
        combined[item[0]] = item[1]

for item in person3:
    if item[0] in combined:
        # How to add data, if key already exists in dict ?
        print('Exists')
    else:
        combined[item[0]] = item[1]

print(combined)

# Current output
# {'Name': 'Alex', 'Gender': 'M', 'Age': 30, 'Phone': '1234567', 'Passport': '987654'}

Will really appreciate if someone can advise how to make it.

CodePudding user response:

You can use defaultdict(list) like below:

from collections import defaultdict

person1 = [['Name','Alex'], ['Gender','M'], ['Age', 30]]
person2 = [['Name','Anna'], ['Gender','F'], ['Phone','1234567'],['Age', 25]]
person3 = [['Name','Fred'], ['Gender','M'], ['Age', 33], ['Passport', '987654']]


dct = defaultdict(list)

def key_in_per(person, key):
    for p in person:
        if key == p[0]:
            return p[1]
    return None

for key in ['Name','Gender', 'Age', 'Phone', 'Passport']:
    for person in [person1, person2, person3]:
        dct[key].append(key_in_per(person, key))

print(dct)
print(pd.DataFrame(dct))

Output:

defaultdict(list,
            {'Name': ['Alex', 'Anna', 'Fred'],
             'Gender': ['M', 'F', 'M'],
             'Age': [30, 25, 33],
             'Phone': [None, '1234567', None],
             'Passport': [None, None, '987654']})


    Name    Gender  Age Phone   Passport
0   Alex    M       30  None    None
1   Anna    F       25  1234567 None
2   Fred    M       33  None    987654
  • Related