Home > Software engineering >  Unpack a list of dictionary and combine multiple values for distinct keys
Unpack a list of dictionary and combine multiple values for distinct keys

Time:02-18

I have following dictionary:
[{'author__name': 'Alexandre Dumas', 'title': 'The Three Musketeer'}, {'author__name': 'Alexandre Dumas', 'title': 'The Count of Monte Cristo'}, {'author__name': 'Leo Tolstoy', 'title': 'Resurrection'}, {'author__name': 'Leo Tolstoy', 'title': 'War and Peace'}, {'author__name': 'Leo Tolstoy', 'title': 'Anna Karenina'}]

I want the output to be
Leo Tolstoy: “War and Peace”, “Anna Karenina”, “Resurrection”
Alexandre Dumas: “The Three Musketeers”, “The Count of Monte Cristo”


What can be the most efficient way to do that. I'm new to python, any sort of help will be very appreciated. Thanks in advance.

CodePudding user response:

I would suggest a defauldict to group the book titles per author, then print as you need

from collections import defaultdict

values = [{'author__name': 'Alexandre Dumas', 'title': 'The Three Musketeer'},
          {'author__name': 'Alexandre Dumas', 'title': 'The Count of Monte Cristo'},
          {'author__name': 'Leo Tolstoy', 'title': 'Resurrection'},
          {'author__name': 'Leo Tolstoy', 'title': 'War and Peace'},
          {'author__name': 'Leo Tolstoy', 'title': 'Anna Karenina'}]

result = defaultdict(list)
for value in values:
    result[value['author__name']].append(value['title'])

for author, titles in result.items():
    print(author, ":", ",".join(f'"{title}"' for title in titles))
Alexandre Dumas : "The Three Musketeer","The Count of Monte Cristo"
Leo Tolstoy : "Resurrection","War and Peace","Anna Karenina"

CodePudding user response:

You can simply use below code:

import pandas as pd
data = [{'author__name': 'Alexandre Dumas', 'title': 'The Three Musketeer'},
        {'author__name': 'Alexandre Dumas', 'title': 'The Count of Monte Cristo'},
        {'author__name': 'Leo Tolstoy', 'title': 'Resurrection'},
        {'author__name': 'Leo Tolstoy', 'title': 'War and Peace'},
        {'author__name': 'Leo Tolstoy', 'title': 'Anna Karenina'}]

pd.DataFrame(data).groupby("author__name")["title"].apply(lambda x: ','.join(x)).reset_index()

Output

author__name title
0 Alexandre Dumas The Three Musketeer,The Count of Monte Cristo
1 Leo Tolstoy Resurrection,War and Peace,Anna Karenina

Explantaion

Using groupby, you can group over whatever column you like. Then by using apply on the title column, you can join the string as exactly as you want.

  • Related