Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we're going to discuss nested lists. Has anyone ever heard of a list within a list?
I think I’ve seen that before! Like a matrix?
"Exactly! A matrix is a great example of nested lists. Let's say we want to represent grades for three students across three subjects. We can use:
Now, how can we change a specific grade in our nested list?
Do we just overwrite it like with normal lists?
"Exactly! For instance, if student one improved and we want to change their second grade to 90, we would write:
Who can give me a real-world scenario where nested lists might be useful?
How about data tables in a database?
Exactly! Nested lists can act like records in a database. Imagine storing customer data in a list, where each inner list represents a different customer’s details.
Can we use them for a reservation system?
"Spot on! We could have a nested list storing reservations by time slots for different days. For instance:
A data structure to store lists within lists, enabling organization of complex data.
Accessing elements through their index, particularly multiple indices in nested lists.
Use of nested lists to represent multi-dimensional data such as matrices.
Example of a nested list representing a classroom of students and their scores:
classroom = [['Alice', [85, 90]], ['Bob', [80, 70]], ['Charlie', [88, 92]]]Use of lists to represent a seating arrangement in a theater:
theater_seating = [['A1', 'A2', 'A3'], ['B1', 'B2'], ['C1', 'C2', 'C3', 'C4']]Nested lists are quite a treat, with layers so neat, inner lists can’t be beat!
Imagine a bookshelf where each shelf holds a box. Each box has books. The bookshelf is like a list, the shelves are lists too, and the books are the values inside those lists.
To remember the concept of nested lists: Think of 'L.I.S.T': Layers of Inner Structures Together.
N.L.S. = Nested Lists Structure, to help remember that items can be nested within one another.
Term
What is a nested list?
Click to flip
Term
How do you access the first element of a nested list?
Click to flip
Term
How do you manipulate a nested list?
Click to flip
A list that contains other lists as its elements, allowing for a multi-dimensional data structure.
A rectangular array of numbers or elements arranged in rows and columns, often represented as a nested list in Python.
Accessing an element in a list through its position number, starting from zero.
Nested lists in Python allow you to store a list within another list, enabling complex data structures like matrices.
In Python, nested lists are lists that contain other lists as their elements. This structure is useful for organizing data in a hierarchical manner, such as matrices or multi-dimensional arrays, enabling easier access and manipulation of complex datasets.
Nested lists are a powerful feature in Python that allow the creation of a list within another list. This functionality can be used to represent complex structures like matrices, grids, or tables, enabling the organization of data in a multi-dimensional way. For instance, the following code creates a 3x3 matrix:
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]To access elements in a nested list, you use multiple indices, as demonstrated below:
print(matrix[0][1]) # Output: 2In this case, matrix[0] accesses the first list, and [1] retrieves the second item from that list.
Nested lists are especially useful in various applications, including data science, where you may need to handle multi-dimensional data structures.