Reputation: 37
I have SQL data from a table in the form (songID, title, artist) Example:
(0, 'Fix You', 'Coldplay')
(1, 'How to Save a Life', 'The Fray')
(2, 'Chasing Cars', 'Snow Patrol')
(3, 'Colide', 'Howie Day')
My task is to format this data into a dictionary called songs so it appears:
songs = {songID: [title, artist], songID: [title, artist], ...}
the database is just called database
Upvotes: 1
Views: 882
Reputation: 19821
If you have data like this:
data = [(0, 'Fix You', 'Coldplay'),
(1, 'How to Save a Life', 'The Fray'),
(2, 'Chasing Cars', 'Snow Patrol'),
(3, 'Colide', 'Howie Day')]
You could convert it to dict as following:
>>> {id: [title, artist] for id, title, artist in data}
{0: ['Fix You', 'Coldplay'],
1: ['How to Save a Life', 'The Fray'],
2: ['Chasing Cars', 'Snow Patrol'],
3: ['Colide', 'Howie Day']}
Upvotes: 2