# update multiple itemsperson = {"name": "Alice"}person.update({"age": 25, "city": "NYC"})#ans: {"name": "Alice", "age": 25, "city": "NYC"}
# remove and return valueperson = {"name": "Alice", "age": 25}age = person.pop("age")#ans: age is 25#ans: person is {"name": "Alice"}
# remove and return last itemperson = {"name": "Alice", "age": 25}item = person.popitem()#ans: item is ("age", 25)
# get or set defaultperson = {"name": "Alice"}age = person.setdefault("age", 0)#ans: age is 0#ans: person is {"name": "Alice", "age": 0}
# remove all itemsperson = {"name": "Alice", "age": 25}person.clear()#ans: {}
# shallow copyperson = {"name": "Alice"}person_copy = person.copy()#ans: person_copy is {"name": "Alice"}
# create dict from keyskeys = ["a", "b", "c"]d = dict.fromkeys(keys, 0)#ans: {"a": 0, "b": 0, "c": 0}
# update behavior?d = {"a": 1}d.update({"b": 2, "a": 10})#ans: {"a": 10, "b": 2}
# pop with default?d = {"a": 1}d.pop("b", 0)#ans: 0
# pop without default?d = {"a": 1}d.pop("b")#ans: KeyError
# setdefault existing key?d = {"a": 1}d.setdefault("a", 10)#ans: 1 (doesn't change existing)
# popitem on empty?d = {}d.popitem()#ans: KeyError
# fromkeys default?keys = ["a", "b"]d = dict.fromkeys(keys)#ans: {"a": None, "b": None}
# copy is shallow?d = {"a": [1, 2]}d2 = d.copy()d2["a"].append(3)#ans: d is {"a": [1, 2, 3]}
# clear return?d = {"a": 1}result = d.clear()#ans: result is None#ans: d is {}
# update from pairs?d = {"a": 1}d.update([("b", 2), ("c", 3)])#ans: {"a": 1, "b": 2, "c": 3}
# multiple setdefault?d = {}d.setdefault("a", []).append(1)d.setdefault("a", []).append(2)#ans: {"a": [1, 2]}
Google tag (gtag.js)