Stock up a nested list
Lists can also contain other lists. In the example shown below, x
is a nested list consisting of three lists:
x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
You can use indexing to subset lists within a nested list. To extract the first list within x
, you can use the following command:
x[0]
[1, 2, 3]
The two lists names
and prices
you created earlier are available in your workspace.
This exercise is part of the course
Introduction to Python for Finance
Exercise instructions
- Create a list named
stocks
consisting of the listsnames
andprices
, in that order. - Use list indexing to subset the list
prices
fromstocks
(Remember thatprices
is the second element instocks
).
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create and print the nested list stocks
stocks = [____, ____]
print(stocks)
# Use list indexing to obtain the list of prices
print(stocks[____])