When to use List vs Dict in Python?

28/10/2022

Lists

my_books = ["Atomic Habits", "Life is what you make it", "power of habits"]

Dictionaries

Used to store Multiple attributes for a single item

my_books = {
            "book_name": "Life is what you make it ",
            "author": "Preethi Shenoy",
            "pages": 150,
           }

List of Lists

my_books = [
    ["Atomic Habits", "James Clear", 200],
    ["Life is what you make it", "Preethi Shennoy", 150],
    ["power of habits", "", 320],
]

List of Dicts

book_as_dict = [
    {"book_name": "Atomic Habits", "author": "James Clear", "pages": 200},
    {
        "book_name": "Life is what you make it ",
        "author": "Preethi Shenoy",
        "pages": 150,
    },
    {"book_name": "LoonShots", "author": "", "pages": 200},

Dict of Dicts

book_as_dict = {
    "Atomic Habits": {
        "book_name": "Atomic Habits",
        "author": "James Clear",
        "pages": 200,
    },
    "Life is what you make it ": {
        "book_name": "Life is what you make it ",
        "author": "Preethi Shenoy",
        "pages": 150,
    },
    "LoonShots": {"book_name": "LoonShots", "author": "", "pages": 200},
}