Lists: Exercises


5. Nested Lists. In Python, you can create nested lists, which are lists within lists. Nested lists are useful for representing structured data, such as matrices or tables.

For example, you can create a nested list to represent a 2x2 matrix: matrix = [[1, 2], [3, 4]].

To access elements in nested lists, you must specify two indices (e.g. matrix[0][1]).

In the exercise below, a matrix has been defined for you. Access the element in the first row and second column.


# Create a nested list to represent a 2x2 matrix. matrix = [[1, 2], [3, 4]] # Access the element in the first row and second column. element = ___ # Print the element. print(element) # Create a nested list to represent a 2x2 matrix. matrix = [[1, 2], [3, 4]] # Access the element in the first row and second column. element = matrix[0][1] # Print the element print(element) test_object("element") success_msg("You have correctly accessed an element in a nested list.")
To create a nested list, place lists inside another list. Use indexing to access elements in the nested list.

Previous Exercise Next Exercise