Tips and Traps¶
Starting from Python 3.7,
dict
preserves insertion order (i.e.,dict
is ordered). There is no need to useOrderedDict
any more in Python 3.7+. However,set
in Python is implemented as an unordered hashset and thus is neither ordered nor sorted. A trick to dedup an iterablevalues
while preserving the order of first occurences is to leveragedict
insteadset
.{v: None for v in values}.keys()
This is also a good trick to yield reproducible dedupped results without using sort.
With the method
dict.setdefault
, you do not really needdefaultdict
. As a matter of fact, it is recommended that you usedict
overdefaultdict
for safty reasons.
s = set([11111, 10, 8, 7, 1, 2, 3])
list(s)
list(s)
import numpy as np
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¶
https://stackoverflow.com/questions/9792664/converting-a-list-to-a-set-changes-element-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