LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Introduction to Python for Finance

Kurs anzeigen

Anleitung zur Übung

  • 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).

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

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

# Use list indexing to obtain the list of prices
print(stocks[____])
Code bearbeiten und ausführen