Chapter 8 Flashcards
What is a set and what can you put in it?
A set is a mutable container of UNORDERED elements (no indices) that are unique (no duplicates).
Created using the set() function.
Holds sequence-type iterable objects (i.e. lists, tuples, strings)
How do you make a set literal?
How do you make an empty set?
set{}
set()
In a set, the index operator…
…is not valid, because a set contains unique unordered elements with no set position.
A set is often used to…
…reduce a list of items that potentially contains duplicates into a collection of unique values
How can you remove duplicates from a list?
Pass the list into a set() to make it into a set of unique values
Format:
ex_list = []
unique_set = set(ex_list)
#copies ex_list to unique_set w/o duplicates
Are sets mutable or immutable?
Mutable
How do you add elements to a set?
How do you remove elements?
What does the .pop function do?
set.add(value)
set.remove(value)
set.pop() removes a random element
How would you add the elements of set_2 to set_1?
How would you clear ALL values in a set?
set1.update(set_2)
set.clear()
This function returns a set containing the common elements shared between set and all provided sets
set.intersection(set_a, set_b, set_c, …)
This function returns a set containing all the unique elements in all given sets.
set.union(set_a, set_b, set_c, …)
Function that returns a set containing only the values in set that are not found in the given argument sets.
set.difference(set_a, set_b, set_c, …)
Function that returns a set containing only values found in exactly one of either set_a or set_b
set_a.symetric_difference(set_b)
What is a dictionary?
What is the object type?
What is its mutability?
A dictionary is a container used to associate/map keys (terms placed in a dict) with values.
Represented by object type dict
Dictionaries are mutable.
What is a key?
What types can be used for a key?
A key is a term that can be located in a dictionary, (such as the word “cat” in the English dictionary), and is associated with a value through the dict.
A key can consist of any immutable type (string, tuple, etc.), the value can be of any type.
What is the syntax for creating a dictionary?
ex_dict =
{
“Example Key 1” : 77
“Example Key 2” : 86
}
What will cause a KeyError when calling an item in a dictionary?
How do you avoid this?
Dictionary elements cannot be indexed, and thus accessing entries by indexing will result in a KeyError.
We avoid this by putting the key itself in brackets where an index would normally be located when accessing.
Are elements in a dictionary ordered or unordered?
Dictionary elements are ordered, despite the fact that they cannot be indexed.
How do you add or modify an entry in a dictionary?
adds key “ex_key” and assigns value “ex_val”
ex_dict[ex_key] = ex_val
How do you delete an entry from a dictionary?
deletes key “ex_key” from dictionary “dict”
del ex_dict[ex_key]
What does the sort() function do?
What rules does it follow?
sort() re-arranges the elements of a list from lowest value to highest.
Numbers are sorted by value, strings are sorted according to ASCII/Unicode values, lists are compared element by element, etc.
What does the sorted() function do?
How does this differ from the sort() function?
sorted() sorts the elements the same as list.sort(), but returns the elements as a new list rather than rearranging the elements of the existing list.
What is the purpose of the optional key argument in the list.sort() and sorted() functions?
The key argument specifies a function to be applied to each element prior to comparing their values.
For example, “key=str.lower” as the key argument will convert all elements to lower case before comparing their values.
When using list.sort() or sorted(), how would you reverse the resulting list of elements?
Use the optional argument: reverse=True
What are the 2 primary ways to create a dict?
1). Wrap curly braces { } around key-value pairs (whether literals or variables).
i.e.: {‘Jose’: ‘A+’, ‘Gino’: ‘C-‘}
2). Use the dict function
i.e.: dict(Bob=’97’, John=’83’)