Bad Code

1
2
3
4
5
6
7
def get_authors_names(posts):
    authors_names = []
    for post in posts:
        author = post["author"]
        author_name = author["name"]
        authors_names.append(author_name)
    return authors_names

Good code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from typing import List, TypedDict

class Author(TypedDict):
    name: str
    email: str
    bio: str
    website: str

class Post(TypeDict):
    title: str
    author: Author
    publication_date: str
    content: str

def get_authors_names(posts: List[Post]) -> List[str]:
    return [post["author"]["name"] for post in posts]