
After this lesson, you will be able to:
Here are some lists:
unique_colors = ["red", "yellow", "red", "green", "red", "yellow"]
subscribed_emails = ["mary@gmail.com", "opal@gmail.com", "mary@gmail.com", "sayed@gmail.com"]What could be a problem here?
Lists:
unique_colors_list = ["red", "yellow", "red", "green", "red", "yellow"]
subscribed_emails_list = ["mary@gmail.com", "opal@gmail.com", "mary@gmail.com", "sayed@gmail.com"]Sets: Lists without duplicates!
unique_colors_set = {"green", "yellow", "red"}
subscribed_emails_set = {"mary@gmail.com", "opal@gmail.com", "sayed@gmail.com"}[] versus the {}.Making a set via a list - Python removes duplicates automatically.
my_set = set(a_list_to_convert)
# In action:
unique_colors_list = ["red", "yellow", "red", "green", "red", "yellow"]
unique_colors_set = set(unique_colors_list)
# => {"green", "yellow", "red"}
# Instead of passing a list in (a_list_to_convert), we could just type it:
my_set_2 = (["enter", "list", "here"])
# In action:
unique_colors_set_2 = set(["red", "yellow", "red", "green", "red", "yellow"])
# => {"green", "yellow", "red"}Making a set directly, in curly braces:
Lists are always in the same order:
my_list = ["green", "yellow", "red"] is always going to be["green", "yellow", "red"]my_list[0] is always "green"; my_list[1] is always "yellow"; my_list[2] is always "red".Sets are not! Like dictionaries, they’re in any order.
my_set = {"green", "yellow", "red"} could later be {"red", "yellow", "green"}!my_set[0] could be "green", "red", or "yellow" - we don’t know!We cannot do: print(my_set[0]) - it could be anything! Python won’t let us.
Let’s pull up a new set_practice.py file and make some sets!
clothing_list containing the main color of your classmates’ clothing.clothing_list, make a set named clothing_set.for loop to print out both clothing_list and clothing_set.How do we add more to a set?
add vs append - this is because we can’t guarantee it’s going at the end!
Let’s a few colors to clothing_list and clothing_set, then print them.
Remember, lists are always the same order: ["green", "yellow", "red"].
my_list[0] is always “green”.Remember, sets are not!
{"green", "yellow", "red"}, my_set[0] could be green, red, or yellow.The same way, we need to be careful about removal:
# In a list:
clothing_list.pop() # Removes and returns the last item in the list.
clothing_list.pop(0) # Removes and returns a specific (here, the first) item in the list.
# In a set
clothing_set.pop() # No! This is unreliable! The order is arbitrary.
clothing_set.pop(0) # No! Python throws an error! You can't index sets.
clothing_set.remove('red') # Do this! Call the element directly!Lists:
[].append(), insert(index), pop(), pop(index).Sets:
{} or with set(my_list).add() and remove(element).### Creation ###
# List
my_list = ["red", "yellow", "green", "red"]
# Sets
my_set = {"red", "yellow", "green"}
my_set2 = set(my_list)
my_set = set(a_list_to_convert)
### Appending a New Value ###
my_list.append("blue")
my_set.add("blue")
### Appending a Duplicate ###
my_list.append("blue")
# => my_list = ["red", "yellow", "green", "red", "blue", "blue"]
my_set.add("blue")
# => my_set = {"red", "yellow", "green", "blue"}
### Removing items: ###
my_list.pop(1)
my_set.remove("red")A set is a type of list which doesn’t allow duplicates.
What if, instead, we have a list we don’t want to change?
We don’t want:
rainbow_colors[0] = ("gray")
## Gray's not in the rainbow!
rainbow_colors.pop()
## We can't lose violet!
rainbow_colors.append("pink")
# Pink's not in the rainbow!We want rainbow_colors to be immutable - the list cannot be changed.
How we do that in Python?
Sets are one specific type of list.
Tuples are another specific type of list.
When should you use a tuple?
().rainbow_colors_tuple = ("red", "orange", "yellow", "green", "blue", "indigo", "violet")
print(rainbow_colors[1])
# Prints "orange"for loop (just like a set or list!).rainbow_colors_tuple = ("red", "orange", "yellow", "green", "blue", "indigo", "violet")
for color in rainbow_colors_tuple:
print(color)Let’s declare a tuple named seasons and set it to have the values fall, winter, spring, and summer. We’ll print the tuple and each value. Then we’ll try to reassign them (we can’t)!
List:
["red", "red", "yellow", "green"].append(), insert(index), pop(), pop(index)Set:
{"red", "yellow", "green"}.add() and remove(element)Tuple:
("red", "red", "yellow", "green") will always be ("red", "red", "yellow", "green").### Creation ###
# List
my_list = ["red", "yellow", "green", "red"]
# Sets
my_set = {"red", "yellow", "green"}
my_set2 = set(my_list))
my_set = set(a_list_to_convert)
# Tuples
my_tuple = ("red", "yellow", "green")
### Appending a New Value ###
my_list.append("blue")
my_set.add("blue")
# Tuples -> You can't!
### Removing items: ###
my_list.pop(1)
my_set.remove("red")
# Tuples -> You can't!Variables certainly can hold a lot!
type() tells us what a variable is: set, tuple, list, dictionary, integer, string - anything!Try it:
Create a local file, sets_tuples.py. In it:
[]), set ({}), and tuple (()) of some of your favorite foods.Next, in every list type that you can:
"pizza" anywhere; append "eggs" to the end."pizza".1 to be "popcorn".2 and re-insert it at index 0.0.Print your final lists using a loop, then print their types. Don’t throw an error!
We’ve learned two new types of lists:
Sets:
Tuples: