3. Lists, Dictionaries & Tuples
The Problem with Variables
Variables are the most essential part of any program. But they are limited by the fact that you can only store one value in them. What if you wanted to store multiple values without creating multiple variables.
name_jake = "Jake"
name_benny = "Benny"
name_nick = "Nick"
name_gunek = "Gunek"
This is where lists, dictionaries, and tuples come in. You can store multiple pieces of data into one variable. This helps you organize and make it easier to code larger projects.
Intro to Lists
One of the three methods is to store multiple pieces of one data type into one variable. Lists are not fixed values, meaning that you can add, delete, and change items inside of them.
list_of_numbers =
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
Creating a List
First, create a name for the list, just like you do with a variable.
list_of_numbers
Then, put down an equal sign and then square brackets.
list_of_numbers = []
Finally, add multiple values by separating them with commas.
list_of_numbers = [12, 4, 5]
List Methods 101
Here are the list methods you can call to add and remove data from it.
Adding to Lists
list_of_numbers.append(99)
This will add the value to the end of the list
Removing from Lists
list_of_numbers.remove(4)
This will remove any instance of the value from the list.
Intro to Tuples
Tuples are exactly like lists. The only difference between them is that lists are modifiable, and tuples are not. This means that lists cannot be changed in any way, unlike lists. When you create a tuple, it remains the same throughout the entire program.
Creating a Tuple
Follow the same steps as a list, but use parenthesis instead of square brackets.
tuple_of_numbers = (12, 4, 5)
Intro to Dictionaries
Dictionaries are very different from lists and tuples. Imagine having multiple variables inside of one variable. That's exactly what a dictionary is.
dictionary =
x = 12,
y = 44,
z = 82,
m = 90,
o = 1,
l = 8
Each dictionary value consists of two components: A key and a value.
"Jenny" : 98
Key
Value
Imagine a key as the variable name, and the value is, well, the value you assign to it.
Creating a Dictionary
First, create a name for the dictionary, just like you do with a variable.
dict_of_names
Then, put down an equal sign and then curly brackets.
dict_of_names = {}
Finally, add multiple values by specifying a key and value for each item in the dictionary.
dict_of_names = {"Hannah":78, "Jake":99}
Accessing List Items
To access specific list items, call the list name, add square brackets, and the index of the item you want. Indexes are like dictionary keys. The first value in a list starts at 0 and increments up by one for every new item in the list.
list_of_numbers[0]
Accessing Tuple Items
Tuples work exactly the same as lists. To access items in a tuple, follow the same steps for the list, but replace the square brackets with parenthesis.
tuple_of_numbers(0)
Accessing Dictionary Items
Dictionaries are a bit different compared to the others. To access dictionary items, call the name of the dictionary, put curly brackets, and inside the curly brackets, call the name of the key, and it will return the value assigned to the key.
dict_of_names{"Hannah"}