Skip to content

Instantly share code, notes, and snippets.

View y56's full-sized avatar
🌏
(* ̄▽ ̄)/‧☆*"`'*-.,_,.-*'`"*-.,_☆

Eugene y56

🌏
(* ̄▽ ̄)/‧☆*"`'*-.,_,.-*'`"*-.,_☆
View GitHub Profile
@y56
y56 / to remove the last entry from orderedDict
Created November 21, 2019 04:31
to remove the last entry from orderedDict
from collections import OrderedDict
myOrderedDict = OrderedDict()
myOrderedDict['a'] = 1
myOrderedDict['b'] = 2
myOrderedDict.popitem(last = False) # Setting last to False signals you wanted to remove the first.
@y56
y56 / Return None if Dictionary key is not available
Last active November 21, 2019 04:26
Return None if Dictionary key is not available
https://stackoverflow.com/questions/6130768/return-none-if-dictionary-key-is-not-available
You can use dict.get()
value = d.get(key)
which will return None if key is not in d. You can also provide a different default value that will be returned instead of None:
value = d.get(key, "empty")
@y56
y56 / What's the difference between dict() and {}?
Last active November 21, 2019 04:22
What's the difference between dict() and {}?
https://stackoverflow.com/questions/664118/whats-the-difference-between-dict-and
>>> from timeit import timeit
>>> timeit("a = {'a': 1, 'b': 2}")
0.424...
>>> timeit("a = dict(a = 1, b = 2)")
0.889...