< Controlling the flow
Lists, Tuples, and Dictionaries
WIP >


In this lesson we will be learning 3 more datatypes: Lists, Tuples, and Dictionaries.

Lists

Lists allow you to store multiple items within one variable. Here’s an example:

animals = ["cat", "dog", "bird"]

As you can see, we can put multiple strings within this one animals variable. Each item is seperated by a comment, and the list is surrounded by [].

You can grab the contents of a list by calling it:

print(animals) # Output: ['cat', 'dog', 'bird']

You can grab a specific item of a list like this:

print(animals[0]) # Output: cat

This will print the first item from this list, which in this case would be cat. Yes, 0 is a valid number here, because lists start at 0 instead of 1.

You can add an item to a list using .append(). For example:

animals.append("horse")

This will add the item "horse" to the end of our animals list.

We can also insert an item to a specific location of our list using .insert():

highScoreOwners = ["Jake", "Sarah"]
print(highScoreOwners) # Output: ['Jake', 'Sarah']
highScoreOwners.insert(1, "Michael")
print(highScoreOwners) # Output: ['Jake', 'Michael', 'Sarah']

This will add the item "Michael" to placement (index) 1, which pushes "Sarah" to index 2.

You can remove an item from a list by value using .remove():

currentSubscriptionHolders = ["Michelle", "John", "Clint"]
print(currentSubscriptionHolders) # Output: ['Michelle', 'John', 'Clint']
currentSubscriptionHolders.remove("John") # Removes the first item with a value of "John"
print(currentSubscriptionHolders) # Output: ['Michelle', 'Clint']

Or, you can remove an item from a list by index using pop():

currentSubscriptionHolders = ["Michelle", "John", "Clint"]
print(currentSubscriptionHolders) # Output: ['Michelle', 'John', 'Clint']
currentSubscriptionHolders.pop(1) # Removes the item at index 1
print(currentSubscriptionHolders) # Output: ['Michelle', 'Clint']

Tuples

A tuple is like a list, but you cannot modify it after it’s created. You create it like this:

planOptions = ("basic", "plus", "premiere")

You can still grab items from it likes lists:

print(planOptions[1])

But you can’t add or remove items from the tuple within the code.

Note that tuples can still be modified before/after the code runs, but you can’t dynamically add an item while the code is running like you can with lists.

Why use tuples?

Since you can’t modify tuples, why even use them instead of lists?

It’s mainly system resources. A list stores extra data in the memory for dynamic resizing and reorganizing. Tuples have no need for that extra data, meaning they take up ~20-30% less data than a list. But, the larger a list, the smaller that difference gets. So if you have thousands or even millions of tiny lists like [x, y], they will take up a lot more memory than the same amount of (x, y) tuples.

Using tuples is also better for code readiblity. If I see a list in my code (or someone else’s code), I know it probably will be modified at some point later on. But if it’s a tuple, I know it’s a static list.

All of this is to say, it’s generally best practice to use tuples for anything you don’t plan to modify while the code is running (like the planOptions list I made earlier), and lists for stuff you do want to modify.

Dictionaries

Now, lists and tuples are great and all, but what happens when you want to be able to identify items with something more discriptive than an index? This is where dictionaries come in.

Dictionaries use key-value pairs. This means each value has a key by which you can grab (call) it. Here’s an example of a list:

favoriteAnimals = {"Janet": "cat", "Ann": "dog", "Doug": "bird"}

Calling an item from a dictionary works largely in the same way as lists and tuples, but instead of grabbing it by ID, you grab it by the key.

print(favoriteAnimals["Ann"]) # Output: dog

To add an item to a dictionary, you do this:

favoriteAnimals["Frank"] = "horse"

Notice how this is pretty similar in syntax to variables.

You can remove an item from a dictionary by using .pop(), but instead of by the index, you use the key:

favoriteAnimals.pop("Janet")

During this lesson we have only been using strings to our lists, tuples, and dictionaries (referred to collectively as Collections), but you can also put intergers, floats, booleans, and even other collections inside them.