CS102 A Sorted Tale - DictReader returns Ordered Dict instead of normal dict format

https://www.codecademy.com/paths/computer-science/tracks/cspath-cs-102/modules/data-structures-and-algorithms-sorting-algorithms/projects/sorted-tale

So basically I failed at the first task which is to print the tittles of bookshelf (imported from a csv file using DictReader). But apparently DictReader now returns a OrderedDict which I have no idea how to convert to a normal Dict.
Below is the output if I print bookself:

[OrderedDict([('title', 'Adventures of Huckleberry Finn'), ('author', 'Mark Twain')]), OrderedDict([('title', 'Best Served Cold'), ('author', 'Joe Abercrombie')]), OrderedDict([('title', 'Dear Emily'), ('author', 'Fern Michaels')]), OrderedDict([('title', 'Collected Poems'), ('author', 'Robert Hayden')]), OrderedDict([('title', 'End Zone'), ('author', 'Don DeLillo')]), OrderedDict([('title', 'Forrest Gump'), ('author', 'Winston Groom')]), OrderedDict([('title', 'Gravity'), ('author', 'Tess Gerritsen')]), OrderedDict([('title', "Hiromi's Hands"), ('author', 'Lynne Barasch')]), OrderedDict([('title', 'Norwegian Wood'), ('author', 'Haruki Murakami')]), OrderedDict([('title', "Middlesex: A Novel (Oprah's Book Club)"), ('author', 'Jeffrey Eugenides')])]

The tutorial video is dated back in 2018 so I think it may worked with the older version of Python but not the current one.
Can anyone help me? Thank you.

dict()

>>> from collections import OrderedDict
>>> a = [OrderedDict([('title', 'Adventures of Huckleberry Finn'), ('author', 'Mark Twain')]), OrderedDict([('title', 'Best Served Cold'), ('author', 'Joe Abercrombie')]), OrderedDict([('title', 'Dear Emily'), ('author', 'Fern Michaels')]), OrderedDict([('title', 'Collected Poems'), ('author', 'Robert Hayden')]), OrderedDict([('title', 'End Zone'), ('author', 'Don DeLillo')]), OrderedDict([('title', 'Forrest Gump'), ('author', 'Winston Groom')]), OrderedDict([('title', 'Gravity'), ('author', 'Tess Gerritsen')]), OrderedDict([('title', "Hiromi's Hands"), ('author', 'Lynne Barasch')]), OrderedDict([('title', 'Norwegian Wood'), ('author', 'Haruki Murakami')]), OrderedDict([('title', "Middlesex: A Novel (Oprah's Book Club)"), ('author', 'Jeffrey Eugenides')])]
>>> b = [{k: v for k, v in dict(x).items()} for x in a]
>>> b
[{'title': 'Adventures of Huckleberry Finn', 'author': 'Mark Twain'}, {'title': 'Best Served Cold', 'author': 'Joe Abercrombie'}, {'title': 'Dear Emily', 'author': 'Fern Michaels'}, {'title': 'Collected Poems', 'author': 'Robert Hayden'}, {'title': 'End Zone', 'author': 'Don DeLillo'}, {'title': 'Forrest Gump', 'author': 'Winston Groom'}, {'title': 'Gravity', 'author': 'Tess Gerritsen'}, {'title': "Hiromi's Hands", 'author': 'Lynne Barasch'}, {'title': 'Norwegian Wood', 'author': 'Haruki Murakami'}, {'title': "Middlesex: A Novel (Oprah's Book Club)", 'author': 'Jeffrey Eugenides'}]
>>> 

We’ve still got the original shape (list of objects) but now our objects are normal dictionaries. Because Python 3 retains insertion order they retain same. Built in list and dict methods apply across the board.

Aside

I got this together by searching and reading. Before now I wouldn’t have known what to do, either. Glad you brought it up; been fun.

1 Like