-1

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.

chirag
  • 153
  • 2
  • 13
  • 2
    Please show us what you’ve tried already, so show the *exact* issue and what effort you have put in so far. – S3DEV Feb 17 '22 at 20:09

2 Answers2

0

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"
azro
  • 53,056
  • 7
  • 34
  • 70
  • 1
    Great work ok this. Encouraging the latest generation of lazy programmers who simply come here for hand-out answers from rep mongers. Excellent work! – S3DEV Feb 17 '22 at 20:10
0

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.

TheFaultInOurStars
  • 3,464
  • 1
  • 8
  • 29
  • Don't say "just use below code", to someone that absolutly don't know what to do? That isn't a simple thing, add explanations – azro Feb 17 '22 at 20:17
  • @azro As you can see I have added an **Explanation** part which I believe is enough. If someone is more interested to know how each of these functions works, he or she can SIMPLY search the documentation. – TheFaultInOurStars Feb 17 '22 at 20:33