{"assignment":{"_schema_version":2,"course_id":37,"date_created":"2022-06-28T19:00:00+00:00","date_modified":"2023-10-13T02:46:01.364951+00:00","extra_instructor_files":"","extra_starting_files":"","forked_id":null,"forked_version":null,"hidden":false,"id":1108,"instructions":"## Lists of Dataclasses\n\n```python list-of-dc\nfrom dataclasses import dataclass\n\n@dataclass\nclass Picture:\n    width: int\n    height: int\n    filename: str\n\npictures = [\n  Picture(256, 256, \"cool_dog.png\"),\n  Picture(128, 512, \"family_photo.png\"),\n  Picture(512, 128, \"help_infographic.png\")\n]\nprint(pictures)\n```\n\nWe've previously learned two main data structures for holding multiple pieces of data in one place.\nLists hold data of the same type, while dataclasses make new types that can hold different kinds of data.\nUp until now, we've only put primitive data into our lists and dataclasses.\nBut now, we will start mixing these datatypes.\nThe chapter begins with one of the most useful and common combinations: lists of dataclasses.\nIn the example shown here, we have a list of `Picture` instances.\nEach element of the list is a Picture, and each Picture has the same type of attributes.\nEach Picture has distinct values, though, allowing us to represent many complicated things in one place.\nA comparable idea is a table or spreadsheet.\nMuch of this chapter is dedicated to showing how we can process this kind of data using the loop patterns we have already seen.\n\n## Nested Dataclasses\n\n```python nested-dc\nfrom dataclasses import dataclass\n\n@dataclass\nclass Address:\n    city: str\n    state: str \n\n@dataclass\nclass Person:\n    name: str\n    home: Address\n    work: Address\n\nalice = Person(\"Alice\",\n  Address(\"Baltimore\", \"MD\"),\n  Address(\"Newark\", \"DE\")\n)\nprint(alice)\nprint(alice.home.state)\n```\n\nDataclasses can be placed not only in lists, but also in other dataclasses.\nWe have actually already seen this with Designer's `World` dataclasses, which had `DesignerObject`s inside.\nBut now we will make own our dataclasses with other dataclasses inside.\nComposing dataclasses allows us to isolate chunks of data into smaller pieces that are easier to think about.\nWe can also reuse those new dataclasses in more than one place.\nNesting dataclasses requires chaining together multiple attribute accesses, which can feel a little strange at first.\n\n## Nested Lists\n\n```python nested-lists\nnumbers_2d = [\n  [100, 95, 97, 88],\n  [55, 66, 75, 94],\n  [99, 95, 97, 92]\n]\n\nprint(numbers_2d[1][2])\n\nfor y, row in enumerate(numbers_2d):\n    for x, number in enumerate(row):\n        print(x, y, number)\n```\n\nIn some ways, nesting lists inside of each other is simpler than nesting dataclasses, since lists are a built-in type.\nHowever, dealing with nested `for` loops can be quite tricky.\nThe example code shown here also incorporates an `enumerate`, which is not necessary unless you want access to the indexes as well.\nCritically, note how we first iterate through each `row`, and then each `number` inside of that `row`.\nAlso demonstrated here is indexing a specific element, which requires chaining together two indexes.\n\n## Heavy Nesting\n\n```python heavy-nesting-example\nfrom dataclasses import dataclass\n\n@dataclass\nclass Address:\n    city: str\n    state: str\n\n@dataclass\nclass Person:\n    name: str\n    duty: str\n    home: Address\n\n@dataclass\nclass Company:\n    boss: Person\n    employees: list[Person]\n\nmy_company = Company(\n  Person(\"Alice\", \"Manager\", Address(\"Nome\", \"AK\")), [\n    Person(\"Bob\", \"Janitor\", Address(\"Newark\", \"DE\")),\n    Person(\"Carol\", \"Coder\", Address(\"Wilmington\", \"DE\")),\n    Person(\"Dave\", \"QA\", Address(\"Baltimore\", \"MD\")),\n])\n```\n\nThere's no end to how much we can nest dataclasses and lists together.\nBy doing so, we can represent truly complex data.\nOf course, this also requires more nesting of control structures and attribute accesses.\nJust like with the other combinations of data structures, all of the same rules apply.\nIt's all just a matter of keeping the data clear in your mind.","ip_ranges":"","name":"8) Primer Reading","on_change":"","on_eval":"","on_run":"","owner_id":1,"owner_id__email":"acbart@udel.edu","points":4,"public":true,"reviewed":false,"sample_submissions":[],"settings":"{\n  \"header\": \"Nesting Data\",\n  \"youtube\": {\n    \"Bart\": \"3W0BTvy2zYU\",\n    \"Amy\": \"zEXsP36PEZE\"\n  },\n  \"video\": {\n    \"Bart\": \"https://blockpy.cis.udel.edu/videos/bakery_nesting_primer-Bart.mp4\",\n    \"Amy\": \"https://blockpy.cis.udel.edu/videos/bakery_nesting_primer-Amy.mp4\"\n  },\n  \"summary\": \"\",\n  \"small_layout\": true\n}","starting_code":"","subordinate":true,"tags":[],"type":"reading","url":"bakery_nesting_primer_read","version":8},"ip":"216.73.216.157","submission":{"_schema_version":3,"assignment_id":1108,"assignment_version":8,"attempts":0,"code":"","correct":false,"course_id":37,"date_created":"2026-05-20T23:50:39.553311+00:00","date_due":"","date_graded":"","date_locked":"","date_modified":"2026-05-20T23:50:39.553311+00:00","date_started":"","date_submitted":"","endpoint":"","extra_files":"","feedback":"","grading_status":"NotReady","id":2037102,"score":0.0,"submission_status":"Started","time_limit":"","url":"submission_url-2c3ccd5b-272d-45e7-84ae-df23541601d0","user_id":2044772,"user_id__email":"","version":0},"success":true}
