Lists, Sets, Dictionaries, and Tuples
Created By: Sean Boerhout
Lists
We've already learned about variables, which allow us to store data in different chunks of memory. But what if there are a whole bunch of varaibles that are actually related to each other? Suppose I wanted to list my favorite animals. I could do it like this:
my_favorite_animal1 = "Tiger"
my_favorite_animal2 = "Turtle"
my_favorite_animal3 = "Elephant"
my_favorite_animal4 = "Dolphin"
But this isn't very nice; the variables are essentially all the same, except holding my next favorite animal. To make this more readable, we can use a list.
my_favorite_animals = ["Tiger", "Turtle", "Elephant", "Dolphin"]
# ^ You must use brackets ^
The cool thing about this is that there is no need to have a bunch of variables, because all the data is essentially held in one.
Indexing
But how do I get each variable out? For that, we need to talk about index. The index of one of each item in a list
is where it lies in the list. It's super simple; the first item in the list has an index of 0, and the rest have an index
of 1 + (previous index). For the example above, "Tiger"
has an index of 0, "Turtle"
has an index of 1, etc.
my_most_favorite_animal = my_favorite_animals[0] # Tigers
print(my_most_favorite_animal)
Tiger
You can also use a negative number as an index... what do you think this means?
my_least_favorite_animal = my_favorite_animals[-1]
print(my_least_favorite_animal)
Dolphin
We can also use colons, to specify a range:
print(my_favorite_animals[:])
print(my_favorite_animals[0:3])
print(my_favorite_animals[1:])
print(my_favorite_animals[:3])
print(my_favorite_animals[:-1])
['Tiger', 'Turtle', 'Elephant', 'Dolphin']
['Tiger', 'Turtle', 'Elephant']
['Turtle', 'Elephant', 'Dolphin']
['Tiger', 'Turtle', 'Elephant']
['Tiger', 'Turtle', 'Elephant']
Modifying Elements
Like normal variables, you can also override certain elements in a list:
my_favorite_animals = ["Tiger", "Turtle", "Elephant", "Dolphin"] # original
my_favorite_animals[0] = "blue whale"
print(my_favorite_animals)
['blue whale', 'Turtle', 'Elephant', 'Dolphin']
And you can do basic arithmetic with them too!
print("My favorite animal is " + my_favorite_animals[0])
My favorite animal is blue whale
bananas_every_day = [2, 1, 1, 3, 2, 5, 6]
print("I ate ", bananas_every_day[1] + bananas_every_day[2], " bananas on Monday and Tuesday")
I ate 2 bananas on Monday and Tuesday
Useful Methods
There are also some cool methods that can be used to modify lists:
- Adds an element to the end of a list
my_favorite_animals.append("Hyena")
print(my_favorite_animals)
['Tiger', 'Turtle', 'Elephant', 'Dolphin', 'Hyena']
- removes an element from a list and returns its output
print(my_favorite_animals.pop(0)) # remove Tiger
print(my_favorite_animals)
Tiger
['Turtle', 'Elephant', 'Dolphin', 'hyena']
- Allows you to insert a value at a specified index
fruits = ["apples", "oranges", "lemons"]
fruits.insert(0, "grapes")
print(fruits)
['grapes', 'apples', 'oranges', 'lemons']
Multi-dimensional lists
You can also put lists in other lists:
my_favorite_ocean_animals = ["Tiger Shark", "Sea Turtle", "Dolphin"]
my_favorite_animals = [my_favorite_ocean_animals, "Tortoise", "Elephant"]
print(my_favorite_animals)
[['Tiger Shark', 'Sea Turtle', 'Dolphin'], 'Tortoise', 'Elephant']
And access elements inside the list inside the list
print("My favorite sea creature is ", my_favorite_animals[0][0])
Tiger Shark
Sets
Sets are just like lists, but have three main exceptions:
- Every element must be unique
- They don't preserve their order
- No indexing (since they have no order!)
Let's compare them with some examples.
making_cereal_list = ["Put cereal in bowl", "Put milk in bowl", "Eat!"] # This is a list
making_cereal_set = {"Put cereal in bowl", "Put milk in bowl", "Eat!"} # This is a set
# ^ Sets use curly braces ^
print(making_cereal_list)
print(making_cereal_set)
['Put cereal in bowl', 'Put milk in bowl', 'Eat!']
{'Eat!', 'Put cereal in bowl', 'Put milk in bowl'}
What? Eat the cereal before putting it in your bowl? That doesn't makes sense!
Let's see how sets stay unique:
making_cereal_list = ["Put cereal in bowl", "Put milk in bowl", "Eat!", "Eat!", "Eat!"] # This is a list
making_cereal_set = {"Put cereal in bowl", "Put milk in bowl", "Eat!", "Eat!", "Eat!"} # This is a list
print(making_cereal_list)
print(making_cereal_set)
['Put cereal in bowl', 'Put milk in bowl', 'Eat!', 'Eat!', 'Eat!']
{'Eat!', 'Put milk in bowl', 'Put cereal in bowl'}
As you can see, there is only one "Eat!"
in the set, but three in the list.
Useful Methods
Here are the counterparts to append()
and pop()
but for sets:
- Adds an element to a set (no particular order)
candy_types = {"Twix", "Airhead", "Kit Kat"}
candy_types.add("Skittles")
{'Twix', 'Kit Kat', 'Skittles', 'Airhead'}
- removes an element from a set
candy_types = {"Twix", "Airhead", "Kit Kat"}
candy_types.remove("Twix")
{'Kit Kat', 'Airhead'}
Dictionaries
Dictionaries are like lists with some extra features. They allow you to associate a name to a value in a structured manner:
about_me = {
'name' : 'Zephyr',
'food' : 'fish',
'siblings' : 5,
'species' : 'cat'
}
print(about_me)
print(about_me['name'])
print(about_me['siblings'])
{'species': 'cat', 'food': 'fish', 'siblings': 5, 'name': 'Zephyr'}
Zephyr
5
size_chart = {
1 : 5
2 : 10
}
print(size_chart[1])
5
Modifying dictionaries
Here are the counterparts to append()
and pop()
but for dictionaries:
- In dictionaries, it is super easy to add data! Just make a new key with its own value:
cookies = {
"smell" : "delicious",
"type" : "Chocolate"
}
cookies["taste"] = "amazing"
print(cookies)
{'type': 'Chocolate', 'smell': 'delicious', 'taste': 'amazing'}
- deletes a key and value from a dictionary
pies = {
"apple" : "good",
"pumpkin" : "terrible"
}
del pies["pumpkin"]
print(pies)
{'apple': 'good'}
Useful methods
- returns all the keys of a dictionary in the form of a list
course = {
"language" : "python",
"students" : 100_000,
"Instructor" : "WV Arduino Club"
}
keys = course.keys()
print(keys)
dict_keys(['language', 'Instructor', 'students'])
- returns all the values of a dictionary in the form of a list
course = {
"language" : "python",
"students" : 100_000,
"Instructor" : "WV Arduino Club"
}
values = course.values()
print(values)
dict_values(['python', 'WV Arduino Club', 100000])
- returns the items of the dictionary as a list
You can use a for loop to access the list as such:
course = {
"language" : "python",
"Instructor" : "WV Arduino Club"
}
for key, values in course.items():
print(f"The course {key} is {value}")
The course Instructor is WV Arduino Club
The course language is python
Tuples
Tuples are nearly axactly like a list, but they can't be modified. Like lists, they also mainting their order (Sets don't!). Also, indexing works exactly the same way:
chemistry_consts = (6.022, 2.998, 6.626)
# ^ Tuples use parens ^
print(chemistry_consts[0])
6.022
As mentioned, you can't modify them:
chemistry_consts = (6.022, 2.998, 6.626)
chemistry_consts.append(8.314)
Line 2: AttributeError: 'tuple' object has no attribute 'append'
append()
method doesn't exist for tuples!
Instead, we can create new tuples that have the elements we want from old tuples.
chemistry_consts = (6.022, 2.998, 6.626)
updated_chemistry_consts = chemistry_consts[0:2]
print(updated_chemistry_consts)
(6.022, 2.998)
Useful methods
What can we do to tuples?
- returns number of times a value is in a certain tuple
chemistry_consts = (6.022, 2.998, 6.626, 2.998)
print(chemistry_consts.count(2.998))
print(chemistry_consts.count(6.022))
2
1
- returns index of a the first instance of a certain item in the tuple
chemistry_consts = (6.022, 2.998, 6.626, 2.998)
print(chemistry_consts.index(2.998))
print(chemistry_consts.index(6.022))
1
0