Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Introduction to Python for Finance

Cursus bekijken

Oefeninstructies

  • Create a list named stocks consisting of the lists names and prices, in that order.
  • Use list indexing to subset the list prices from stocks (Remember that prices is the second element in stocks).

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Create and print the nested list stocks
stocks = [____, ____]
print(stocks)

# Use list indexing to obtain the list of prices
print(stocks[____])
Code bewerken en uitvoeren