CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List
Python Code Walkthrough #40

Counting Words With A Dict

mediumDictionaries

📄 Code

Read carefully — what does this print?
1counts: dict[str, int] = {}
2for word in ['red', 'blue', 'red', 'green', 'red']:
3 if word in counts:
4 counts[word] = counts[word] + 1
5 else:
6 counts[word] = 1
7print(counts)
8print(counts['red'])

🎯 Your Answer