You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# the syntax is# my_list[start:stop]# this includes the start position but goes UP TO the stop# you can leave either empty to go to the front or endfirst_two_pets=my_pets[:2]
last_two_pets=my_pets[1:]
Test if a value is inside a list
## with any collection, you can test if an item is inside the collection## it is with the "in" keywordmy_pets= ['euclid', 'leta']
if'euclid'inmy_pets:
print("Euclid is a pet!")
Sets
Create a set or convert a list to a set
my_pet_list= ['euclid', 'leta']
# you can convert lists to sets using the set keywordmy_pet_set=set(my_pet_list)
# sets are like lists but you can't index into them or slice them# they are used for fast membership testing# you can create a new set by:my_pet_set=set(['euclid', 'leta'])
Add an item to a set
my_new_set=set()
# instead of append, like a list, you use 'add'my_new_set.add("Potatoes")
Using sets to enforce uniqueness
my_grocery_list= ['potatoes', 'cucumbers', 'potatoes']
# now if you want to make sure items only appear once, you can convert it to a set# it will automatically do this for you, because items are only allowed to be in sets one timemy_grocery_set=set(my_grocery_list)