1/9
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No study sessions yet.
dictionary
-data structure that consists of key value pairs
-known as associative arrays in some other language, are indexed by keys rather than a numerical index
-keys can be immutable type: number, strings, booleans, etc.
-values can be whatever you want
-empty dict= {}
-dict()
-retrieve values using dict[key]
dict.get()
the get() method will look for a given key in a dictionary. if the key exists, it will return the corresponding value. otherwise it returns None
pop()
method accepts a key and will delete the corresponding key-value pair in the dictionary. it returns the deleted value
popitem()
deletes the most recently added key-value pair. it returns the item as a tuple
clear()
deletes all items from a dictionary. it returns None
del statement
remove items from a dictionary. not a method
update
update method will update a dictionary using the key value pairs from a second dictionary, passed as the argument
** trick
we can use two stars ** to combine multiple dictionaries into a new resulting dictionary
dict union
python 3.9 added the diction union operator(|) it will return a new dict containing the items from the left and right dicts. in the case of duplicated keys, the right side “wins”