Lists in Lists

Introduction to Python for Finance

Adina Howe

Instructor

Lists in Lists

  • Lists can contain various data types, including lists themselves.

  • Example: a nested list describing the month and its associated consumer price index

      cpi = [['Jan', 'Feb', 'Mar'], [238.11, 237.81, 238.91]]
    
Introduction to Python for Finance

Subsetting Nested Lists

months = ['Jan', 'Feb', 'Mar']
print(months[1])
'Feb'
cpi = [['Jan', 'Feb', 'Mar'], [238.11, 237.81, 238.91]]
print(cpi[1])
[238.11, 237.81, 238.91]
Introduction to Python for Finance

More on Subsetting Nested Lists

How would one subset out a specific price index?

cpi = [['Jan', 'Feb', 'Mar'], [238.11, 237.81, 238.91]]
print(cpi[1])
[238.11, 237.81, 238.91]
print(cpi[1][0])
238.11
Introduction to Python for Finance

Let's practice!

Introduction to Python for Finance

Preparing Video For Download...