-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollection Module - defaultdict.py
More file actions
85 lines (34 loc) · 926 Bytes
/
Collection Module - defaultdict.py
File metadata and controls
85 lines (34 loc) · 926 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
# coding: utf-8
# # Collection Module
#
# In[3]:
# default dict
# default dict provides all method that comes from a dictionary but also takes first arguement as a default datatype
# defaultdict never raise a keyerror any key that does not exist get the value return by default factory
# In[4]:
from collections import defaultdict
# In[6]:
# simple dict
d = {'k1' : 1}
# In[7]:
# when use d['k2'] it will throw key error
d['k1']
# In[8]:
d = defaultdict(object)
# In[9]:
# it will not throw an error
d['one']
# In[10]:
# we can also use this by default values use that conjuction using lambda functions
d = defaultdict(lambda : 0)
# In[13]:
# it will reutn 0 when their is not key
d['one']
# In[14]:
d['two'] = 2
# In[15]:
d
# In[16]:
# it will automatically assign key pair values with default value when try to the key which is not in dict
# In[ ]: