Tips and Traps¶
Starting from Python 3.7,
dictpreserves insertion order (i.e.,dictis ordered). There is no need to useOrderedDictany more in Python 3.7+. However,setin Python is implemented as an unordered hashset and thus is neither ordered nor sorted. A trick to dedup an iterablevalueswhile preserving the order of first occurences is to leveragedictinsteadset.{v: None for v in values}.keys()This is also a good trick to yield reproducible dedupped results without using sort.
Construct dict¶
Dictionary Comprehension¶
d = {i: i * i for i in range(3)}
d
Tuple to Dict¶
You can pass an iterable of pairs (a pair is tuple with 2 elements) to dict to create a dict object.
The first elements of pairs are the keys
and the second elements of pairs are the corresponding values.
d = dict((row[0], (row[1], row[2])) for row in [("Ben", 1, 2), ("Lisa", 2, 3)])
d
[k for k in d]
d.items()
Passing an iterable of tuples with lengths differ from 2 causes an ValueError.
dict([("abc", 1, 2)])
Passing an empty iterable to dict generates an empty dict object.
dict([])
pandas.Index is a dict-like Object¶
import pandas as pd
df = pd.DataFrame({"x": [1, 2, 3, 4, 5], "y": [5, 4, 3, 2, 1], "z": [1, 1, 1, 1, 1]})
df.head()
df.index.intersection(d.keys())
KeyError exception is raise is the key is not found.
DefaultDict does not raise an exception when a key is not found
but instead returns the default value.
d[3]
get is the safe version.
It's equivalent to the following code.
d[3] if 3 in d else None
d.get(3)
Merge Two Dictionaries¶
x = {"a": 1, "b": 2}
y = {"b": 3, "c": 4}
{**x, **y}
keys¶
d = {"a": 1, "b": 2}
d.keys()
in¶
d = {"a": 1, "b": 2}
"a" in d
values¶
d = dict((row[0], (row[1], row[2])) for row in [("Ben", 1, 2), ("Lisa", 2, 3)])
d.values()
list(v[0] for v in d.values())
max(v[0] for v in d.values())
Iterate Dictionary¶
Iterate Keys¶
d = {"a": 1, "b": 2}
for k in d:
print(str(k) + ": " + str(d[k]))
Iterate Key/Value Pairs¶
for k, v in d.items():
print(str(k) + ": " + str(v))
Cannot interate (key, value) pairs.
for k, v in d:
print(str(k))
Iterate Values Directly¶
for v in d.values():
print(v)
setdefault - Set Default Value for a Key¶
With the method dict.setdefault,
you do not really need defaultdict.
As a matter of fact,
it is recommended that you use dict over defaultdict for safty reasons.
dic = {"x": 10, "y": 20}
dic
dic.setdefault("x", 0)
dic["x"] += 1
dic
dic.setdefault("z", 0)
dic["z"] += 1
dic
dic.setdefault("list", [])
dic["list"].append("how")
dic
Remove Elements¶
d = {"a": 1, "b": 2}
d
del d["a"]
d
del d["non_exist_key"]
d.pop("non_exist_key", None)
d
Dedup List and Preserve Order¶
words = [
"how",
"are",
"how",
"are",
"how",
"you",
"are",
"how",
"you",
"are",
"you",
"how",
]
You can use set or numpy.unique to dedup a list but it does not preserver the order of first occurence of elements.
list(set(words))
np.unique(words)
One possible way to dedup and preserve the original order of first occurences of elements is to dedup using a dictionary (which preserves insertion order).
" ".join({word: None for word in words})
Sort a Dict¶
Sort a dict object by its keys.
dic = {"how": 2, "are": 4, "you": 3, "doing": 1, "today": 0}
sorted(dic)
sorted(dic.items())
Sort a dict object by its values.
x = {"how": 2, "are": 4, "you": 3, "doing": 1, "today": 0}
dict(sorted(x.items(), key=lambda item: item[1]))
Ref vs Copy¶
d = {"Ben": [1, 2], "Lisa": [2, 3]}
ben = d["Ben"]
ben
ben[0] = 10000
ben
d
d = {"Ben": 1, "Lisa": 2}
ben = d["Ben"]
ben
ben = 10000
ben
d
x = 1
x += 10
x