Lesson 10. Data Structures Part I: List

Most programming languages have the concept of an array. Arrays are sequences of values usually all of the same data type.

Python took the concept of an array and made it much more powerful by creating three different data structures. Each structure has it’s uses which we will explore in later lessons.

In the next three lessons, we are going to talk about how to create these unique data structures and how to work with them. The first structure we are going to talk about is called a list.

A list is the closest structure to a classic array. Arrays have methods that allow you to work with the data stored in them.

Examples

Example #1 Creating List

Unlike simpler data types, list can be created without initial values.

In [4]:
empty_list = []

fighters = ['Eagle', 'Falcon', 'Tomcat']
print(fighters)
['Eagle', 'Falcon', 'Tomcat']

Example #2 Adding Values To An Existing List

In [5]:
fighters.append('Lightning II')
print(fighters)
['Eagle', 'Falcon', 'Tomcat', 'Lightning II']

Example #3 Accessing Values In A List

List values are accessed using an index number that represents the position of a value in the list. The index starts at 0 and the last value can be accessed using an index value of -1.

In [6]:
print(fighters[2])
print(fighters[-1])
Tomcat
Lightning II

Copyright © 2020, Mass Street Analytics, LLC. All Rights Reserved.