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.

2 Likes

Hi guys,

In the “A Sorted Tale” project of the Computer Science path, could anyone please help me understand why in the function def by_total_length

The below works:

def by_total_length(book_a, book_b):
  return len(book_a['author_lower']) + len(book_a['title_lower']) > len(book_b['author_lower']) + len(book_b['title_lower'])

but the below gets me an “int object is not iterable” ?

  return sum(len(book_a['author_lower']), len(book_a['title_lower'])) > sum(len(book_b['author_lower']), len(book_b['title_lower']))

Documentation for sum (https://docs.python.org/3/library/functions.html#sum):

sum(iterable, /, start=0 )

Sums start and the items of an iterable from left to right and returns the total. The iterable’s items are normally numbers, and the start value is not allowed to be a string.

# Integers are not iterables
sum(3, 5) > sum(6, 1)
# TypeError: 'int' object is not iterable

# Lists are iterables
sum([3,5]) > sum([6,1])
# True

# Tuples are iterables
sum((3,5)) > sum((6,1))
# True

1 Like