0
0
Pythonprogramming~20 mins

Nested lists in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Working with Nested Lists in Python
๐Ÿ“– Scenario: You are organizing a small library's book shelves. Each shelf holds a list of book titles. The library has three shelves.
๐ŸŽฏ Goal: You will create a nested list representing the shelves and their books, then count how many books are on each shelf, and finally print the counts.
๐Ÿ“‹ What You'll Learn
Create a nested list called library_shelves with exactly three inner lists, each containing the exact book titles specified.
Create a list called book_counts to store the number of books on each shelf.
Use a for loop with the variable shelf to iterate over library_shelves.
Append the length of each shelf to book_counts.
Print the book_counts list.
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Nested lists help organize grouped data like shelves of books, classrooms of students, or menus with categories.
๐Ÿ’ผ Career
Understanding nested lists is important for data organization, processing multi-level data, and working with complex datasets in many programming jobs.
Progress0 / 4 steps
1
Create the nested list of shelves
Create a nested list called library_shelves with three inner lists. The first list should have 'The Hobbit' and '1984'. The second list should have 'Pride and Prejudice', 'To Kill a Mockingbird', and 'The Great Gatsby'. The third list should have 'Moby Dick'.
Python
Need a hint?

Remember, a nested list is a list inside another list. Use square brackets [] for each shelf.

2
Create the list to hold book counts
Create an empty list called book_counts to store the number of books on each shelf.
Python
Need a hint?

Use [] to create an empty list named book_counts.

3
Count books on each shelf
Use a for loop with the variable shelf to iterate over library_shelves. Inside the loop, append the number of books on the current shelf to the book_counts list using len(shelf).
Python
Need a hint?

Use for shelf in library_shelves: to loop. Use len(shelf) to get the number of books.

4
Print the book counts
Write a print statement to display the book_counts list.
Python
Need a hint?

Use print(book_counts) to show the list of counts.