In 2.7+:
import collections
letters = collections.Counter('google')
Earlier (2.5+, that’s ancient by now):
import collections
letters = collections.defaultdict(int)
for letter in word:
letters[letter] += 1
More Related Contents:
- How to map dict into another dict Python
- Create a list of names whose height is higher than 160 – Explanation on nested dicts cycling [closed]
- Determine the type of an object?
- Why can’t I use a list as a dict key in python?
- multiprocessing: How do I share a dict among multiple processes?
- I’m getting Key error in python
- Getting a list of values from a list of dicts
- Merge and sum of two dictionaries
- Creating a list of dictionaries results in a list of copies of the same dictionary
- Accessing elements of Python dictionary by index
- Intersecting two dictionaries
- How can I make a dictionary from separate lists of keys and values?
- Python creating a dictionary of lists
- Is a Python dictionary an example of a hash table?
- Removing Duplicates From Dictionary
- Check if value already exists within list of dictionaries?
- Convert list into a dictionary [duplicate]
- Sort nested dictionary by value, and remainder by another value, in Python
- Float values as dictionary key
- Most Pythonic Way to Build Dictionary From Single List
- How to pass dictionary items as function arguments in python? [duplicate]
- Python 3 dictionary with known keys typing
- Python: get a dict from a list based on something inside the dict
- How to sort dictionary by key in numerical order Python
- JSON object must be str, bytes or bytearray, not dict
- How to convert a pymongo.cursor.Cursor into a dict?
- Are there any reasons not to use an OrderedDict?
- Dictionary vs Object – which is more efficient and why?
- Changing one dict changes all dicts in a list of dicts [duplicate]
- How do I send list of dictionary as Body parameter together with Files in FastAPI?