Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert a List of Tuples into Dictionary with Grouped Dict Values in Python

Lets say I have a list of tuples like the following

l: list[tuple] = [
    ('bobs trucking', 'ID1'),
    ('bobs trucking', 'ID2'),
    ('bobs trucking', 'ID3'),
    ('bobs groceries', 'ID6'),
    ('bobs groceries', 'ID7'),
    ('bobs groceries', 'ID8'),
    ('bobs groceries', 'ID9')
]

I’d like to group the repeated first tuple elements (e.g. ‘bobs trucking’) into a dictionary with values that have the grouped IDs concatenated like a CSV but for every n values for each item in a list

# An example of how I'd accomplish this (but need a way to do it for every n elements)
l2: list[str] = ['ID1','ID2','ID3']
csv_delim: str = ','.join(l2)

Every unique key has a list where each list item is a CSV string of size n=3

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

# Desired ultimate result
bobs_stuff: dict = {
    'bobs trucking': ['ID1,ID2,ID3'],
    'bobs groceries': ['ID6,ID7,ID8','ID9']
}

Note: I’d like to do this without Pandas.

>Solution :

Iterate over your list of tuples and populate a dictionary with its contents as follows:

list_of_tuples = [
    ('bobs trucking', 'ID1'),
    ('bobs trucking', 'ID2'),
    ('bobs trucking', 'ID3'),
    ('bobs groceries', 'ID6'),
    ('bobs groceries', 'ID7'),
    ('bobs groceries', 'ID8'),
    ('bobs groceries', 'ID9')
]

bobs_stuff = {}

N = 3
td = {}

for k, v in list_of_tuples:
    td.setdefault(k, []).append(v)

for k, v in td.items():
    for i in range(0, len(td[k]), N):
        bobs_stuff.setdefault(k, []).append(','.join(v[i:i+N]))

print(bobs_stuff)

Output:

{'bobs trucking': ['ID1,ID2,ID3'], 'bobs groceries': ['ID6,ID7,ID8', 'ID9']}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading