Lists, Tuples, Dictionaries, and Sets Flashcards
what command do we use to add multiple new elements to a list?
listname.extend([item 1, item 2])
what command do we use when we want to add one new nested element to a list?
listname.append([item 1, item 2])
what is the major difference between lists and tuples?
list items can be individually changed by using listname[item number] = new value
tuples cannot be changed, but instead a new variable must be made such as sortedtuple
what command removes a single item from a list
del listname[0]
true or false: if you have aliased lists and you change an item on list A, it will change on list B
true - they are both pulling from the same object
clone the list to fix the problem
what is the command to clone a list
B = A[:]
how do you sort the values in a tuple
newtuple = sorted(tuple)
how do you access the nested tuple within a tuple
tuple[1][2]
how do you locate the index of a particular string or item?
tuplename.index(“item name”)
how do you create a dictionary?
Dict = {key1:value 1, key 2:value 2}
how do you recall a value from the dictionary using its key
Dict[“key1”]
what is the command to get all the keys in a dictionary?
dictionaryname.keys()
what is the command to get all the values in a dictionary?
dictionaryname.values()
how do you add a key value pair into a dictionary?
dictionaryname[“new key”] = “new value”
how do we delete an entry in a dictionary with its key
del(dictionaryname[“key”])
how do we check if a key is in a dictionary?
“key name” in dictionaryname
returns True or False
how do you convert a list to a set?
newset = set(old_list)
how do we add something to a set?
setname.add(“new item”)
how do we get rid of something in a set?
setname.remove(“item to be removed”)
how do we isolate common elements between sets
set1 & set 2
how do we find items only contained in set 1 but not set 2?
set1.difference(set2)
what is the command to find the crossover between two sets without combining them?
set1.intersection(set2)
how do you combine every element of two sets into one big set
set1.union(set2)
how do we check if a set is fully contained within a different set?
smallset.issubset(largeset)
or
set({“item 1”, “item 2”}).issubset(largeset)
how do we check whether a set in contained in a superset
largeset.issuperset(smallset)