Search code examples
pythonlistdictionarylist-manipulation

Inserting elements of one list into another list if some other element matches


I can't quite figure this out.

Here is my current output:

['Direct General Auto Insurance', 'IT Project Manager', 'Nashville', 'TN', '(615) 331-7540']
['Direct General Auto Insurance', 'IT Systems Manager', 'Nashville', 'TN', '(615) 340-6291']
['Direct General Auto Insurance', 'Junior web Developer', 'Nashville', 'TN', '(615) 399-4700']
['Grand Slam Universal, LLC', 'IT Specialist', 'Nashville', 'TN', '(615) 457-3516']
['Ingram Content Group', 'Desktop Engineer', 'La Vergne', 'TN', '(615) 793-5000']
['Direct General Auto Insurance', 'Data Scientist', 'Nashville', 'TN', '(615) 726-3091']
['Direct General Auto Insurance', 'Janitor', 'Nashville', 'TN', '(615) 831-2600']

The headers are FIRMNAME, JOBTITLE, CITY, STATE, NUMBER.

For the same FIRMNAMEs I need all corresponding JOBTITLEs in the same row. Thus, if FIRMNAMEs match, then add the corresponding JOBTITLEs to that row.

So the output will look like this:

['Direct General Auto Insurance', 'IT Project Manager, IT Systems Manager, Junior Web Developer, Data Scientist, Janitor', 'Nashville', 'TN', '(615) 331-7540']
['Grand Slam Universal, LLC', 'IT Specialist', 'Nashville', 'TN', '(615) 457-3516']
['Ingram Content Group', 'Desktop Engineer', 'La Vergne', 'TN', '(615) 793-5000']

I tried looping over it and using .insert() if there was a matching FIRMNAME already in the list, but couldn't quite figure out the logic.

Thanks for the help.

edit:

This is the desired output:

['Direct General Auto Insurance', 'IT Project Manager, IT Systems Manager, Junior Web Developer, Data Scientist, Janitor', 'Nashville', 'TN', '(615) 331-7540']
['Direct General Auto Insurance', 'IT Project Manager, IT Systems Manager, Junior Web Developer, Data Scientist, Janitor', 'Nashville', 'TN', '(615) 340-6291']
['Direct General Auto Insurance', 'IT Project Manager, IT Systems Manager, Junior Web Developer, Data Scientist, Janitor', 'Nashville', 'TN', '(615) 399-4700']
['Direct General Auto Insurance', 'IT Project Manager, IT Systems Manager, Junior Web Developer, Data Scientist, Janitor', 'Nashville', 'TN', '(615) 726-3091']
['Direct General Auto Insurance', 'IT Project Manager, IT Systems Manager, Junior Web Developer, Data Scientist, Janitor', 'Nashville', 'TN', '(615) 831-2600']
['Grand Slam Universal, LLC', 'IT Specialist', 'Nashville', 'TN', '(615) 457-3516']
['Ingram Content Group', 'Desktop Engineer', 'La Vergne', 'TN', '(615) 793-5000']

I want to retain all numbers.


Solution

  • Use a dict to index the table. For each row, check if you've already seen the company and do the insertion there. If you need to get more complicated (for instance, same company with different locations) then the index key can become a tuple of the unique information.

    my_list = [
        ['Direct General Auto Insurance', 'IT Project Manager', 'Nashville', 'TN', '(615) 331-7540'],
        ['Direct General Auto Insurance', 'IT Systems Manager', 'Nashville', 'TN', '(615) 340-6291'],
        ['Direct General Auto Insurance', 'Junior web Developer', 'Nashville', 'TN', '(615) 399-4700'],
        ['Grand Slam Universal, LLC', 'IT Specialist', 'Nashville', 'TN', '(615) 457-3516'],
        ['Ingram Content Group', 'Desktop Engineer', 'La Vergne', 'TN', '(615) 793-5000'],
        ['Direct General Auto Insurance', 'Data Scientist', 'Nashville', 'TN', '(615) 726-3091'],
        ['Direct General Auto Insurance', 'Janitor', 'Nashville', 'TN', '(615) 831-2600'],
    ]
    
    index = {}
    
    for item in my_list:
        existing = index.get(item[0])
        if not existing:
            index[item[0]] = item
            # or this, if you want don't want to modify the existing table
            # index[item[0]] = item[:]
        else:
            existing.insert(1, item[1])
    
    for item in sorted(index.values()):
        print item
    

    EDIT

    Your edited result is quite different - you want to insert all jobs into each of the firm's original entries. In this case, we just need to build up a list of jobs per firm and then go back to the original list and insert them.

    import collections
    
    my_list = [
        ['Direct General Auto Insurance', 'IT Project Manager', 'Nashville', 'TN', '(615) 331-7540'],
        ['Direct General Auto Insurance', 'IT Systems Manager', 'Nashville', 'TN', '(615) 340-6291'],
        ['Direct General Auto Insurance', 'Junior web Developer', 'Nashville', 'TN', '(615) 399-4700'],
        ['Grand Slam Universal, LLC', 'IT Specialist', 'Nashville', 'TN', '(615) 457-3516'],
        ['Ingram Content Group', 'Desktop Engineer', 'La Vergne', 'TN', '(615) 793-5000'],
        ['Direct General Auto Insurance', 'Data Scientist', 'Nashville', 'TN', '(615) 726-3091'],
        ['Direct General Auto Insurance', 'Janitor', 'Nashville', 'TN', '(615) 831-2600'],
    ]
    
    index = collections.defaultdict(list)
    
    # generate list of jobs per firm
    for item in my_list:
        index[item[0]].append(item[1])
    
    # insert into existing table
    for item in my_list:
        del item[1]
        item[1:2] = index[item[0]]
    
    for item my_list:
        print item