The following will create a dictionary that maps each author's name to alist
of books they've written. This is done using a specialization of the built-in dictionary type nameddefaultdict
which is defined in thecollections
module.
from collections import defaultdict
from pprint import pprint
books = [['Shakespeare', 'William', 'Rome And Juliet', '5', '5.99'],
['Shakespeare', 'William', 'Macbeth', '3', '7.99'],
['Dickens', 'Charles', 'Hard Times', '7', '27.00'],
['']]
d = defaultdict(list)
for book in (book for book in books if book[0]):
d[book[0], book[1]].append(book[2:])
pprint(d)
Output:
{('Dickens', 'Charles'): [['Hard Times', '7', '27.00']],
('Shakespeare', 'William'): [['Rome And Juliet', '5', '5.99'],
['Macbeth', '3', '7.99']]}