Skip to content

Commit 93d124f

Browse files
committed
Simplified Counting with get()
1 parent f488a86 commit 93d124f

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

Dictionary/main4.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Simplified Counting with get()
2+
3+
# We can use get() and provide a default value of zero when the key is
4+
# not yet in the dictionary - and then just add one
5+
6+
counts = dict()
7+
names = ['csev' , 'cwen' , 'csev' , 'zqian' , 'cwen']
8+
for name in names:
9+
counts[name] = counts.get(name, 0) + 1
10+
print(counts)
11+
12+
# Output: {'csev': 2, 'cwen': 2, 'zqian': 1}

0 commit comments

Comments
 (0)