1/41
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
dictionary
{key:value}. an unordered collection which stores key-value pairs that map immutable keys to values.
set
an unordered collection of unique immutable elements
dictionary's keys
Keys must be immutable and unique.
Multiple keys can have the same value
An empty dictionary can be created with
{}
Check if dictionary is empty
Can use a dictionary as a condition to determine if it’s empty—non-empty is True and empty is False
if dictionary:
print('dictionary is not empty')
else:
print('dictionary is empty’)
dictionary.items()
returns each key-value pair as a tuple
Adding a New Key-Value Pair
Assigning to a nonexistent key inserts a new key-value pair.
Remove a key-value pair with
del or pop
dictionary.get(key,2ndargument)
returns its argument’s corresponding value or None if the key is not found
IPython does not display anything for None.
get with a second argument returns the second argument if the key is not found.
dictionary.keys()
Returns keys
dictionary.values()
Returns values
list(dictionary.keys()) or list(dictionary.values()) or list(dictionary.items())
Converting Dictionary Keys, Values and Key-Value Pairs to Lists
Processing Keys in Sorted Order
for a in sorted(dictionaryname.keys()):
== is True if both dictionaries have
the same key-value pairs, regardless of the order in which those key-value pairs were added to each dictionary.
from collections import Counter
Counter()
A counter is a customized dictionary that receives an iterable and summarizes its elements
dictionary.update()
Can insert and update key-value pairs.
Method update also can receive an iterable object containing key-value pairs, such as a list of two-element tuples.
set
{ , } an unordered collection of unique values. do not support indexing and slicing. great for duplicate elimination
Is there a significance to iterating through a set?
No
To create an empty set, must use the set(), because
{} represents an empty dictionary.
Python displays an empty set as set() to avoid confusion with an empty dictionary {}
Proper subset operand?
<
{set1} < {set2}
What does proper subset mean?
all the elements in the left operand are in the right operand, and the sets are not equal
Improper subset operand?
<=
{set1} <= {set2}
Another improper subset method?
{set1}.issubset({set2})
What does improper subset mean?
all the elements in the left operand are in the right operand, and the sets might be equal
Proper superset operand?
>
{set1} > {set2}
What does proper superset mean?
all the elements in the right operand are in the left operand, and the left operand has more elements
Improper superset operand?
>=
{set1} >= {set2}
Another improper superset method?
{set1}.issuperset({set2})
What does improper superset mean?
all the elements in the right operand are in the left operand, and the sets might be equal
The argument to issubset or issuperset
can be any iterable. When either of these methods receives a non-set iterable argument, it first converts the iterable to a set, then performs the operation.
Union
a set consisting of all the unique elements from both sets.
The ___ operator requires two sets, but method ___ may receive any iterable as its argument (this is true for subsequent methods in this section as well).
Ways to find union
{set1} | {set2}
{set1}.union([list])
Intersection
a set consisting of all the unique elements that the two sets have in common
Ways to find intersection
{set1} & {set2}
{set1}.intersection([list])
Difference
a set consisting of the elements in the left operand that are not in the right operand
Ways to find difference
{set1} - {set2}
{set1}.difference([list])
set.add()
inserts its argument if the argument is not already in the set; otherwise, the set remains unchanged
set.remove()
removes its argument from the set—raises a KeyError if the value is not in the set
set.discard()
removes its argument from the set but does not cause an exception if the value is not in the set
set.pop()
Can remove an arbitrary set element and return it with pop
set.clear()
Empties the set
Set comprehensions are defined in
{}