With Python 2.7+, you can use collections.Counter
.
Otherwise, see this counter receipe.
Under Python 2.7+:
from collections import Counter
input = ['a', 'a', 'b', 'b', 'b']
c = Counter( input )
print( c.items() )
Output is:
[(‘a’, 2), (‘b’, 3)]
Related Contents:
- How to count the number of words in a sentence, ignoring numbers, punctuation and whitespace?
- How to return dictionary keys as a list in Python?
- Print a list in reverse order with range()?
- How to delete an item in a list if it exists?
- Python – Count elements in list [duplicate]
- How do I loop through a list by twos? [duplicate]
- Converting Dictionary to List? [duplicate]
- What is the difference between `sorted(list)` vs `list.sort()`?
- Python find elements in one list that are not in the other [duplicate]
- Why does this code for initializing a list of lists apparently link the lists together? [duplicate]
- Remove duplicate dict in list in Python
- How to remove multiple indexes from a list at the same time? [duplicate]
- Why are trailing commas allowed in a list?
- How Big can a Python List Get?
- Iterate through pairs of items in a Python list [duplicate]
- Flatten list of lists [duplicate]
- How to allow list append() method to return the new list
- Pandas DataFrame stored list as string: How to convert back to list
- Zip with list output instead of tuple
- How can I initialize a dictionary of distinct empty lists in Python?
- Loop through list with both content and index [duplicate]
- How to split elements of a list?
- Slicing a list in Python without generating a copy
- Find all index position in list based on partial string inside item in list
- How to remove \n from a list element?
- How do you pick “x” number of unique numbers from a list in Python?
- How do I serialize a Python dictionary into a string, and then back to a dictionary?
- list() uses slightly more memory than list comprehension
- Detecting consecutive integers in a list [duplicate]
- Merge lists that share common elements
- Python Count Elements in a List of Objects with Matching Attributes
- Reserve memory for list in Python?
- Split a list into parts based on a set of indexes in Python
- How to create a list of empty lists
- Grouping Python dictionary keys as a list and create a new dictionary with this list as a value
- Use endswith with multiple extensions
- Select value from list of tuples where condition
- How to access a column in a list of lists in python
- Merge a list of pandas dataframes
- Remove object from a list of objects in python
- Join float list into space-separated string in Python
- join two lists of dictionaries on a single key
- Unpack list to variables
- Python: Unpacking an inner nested tuple/list while still getting its index number
- set of list of lists in python
- Change a string of integers separated by spaces to a list of int [duplicate]
- How to subclass Python list without type problems?
- How can I save a list of dictionaries to a file?
- call list of function using list comprehension
- “TypeError: ‘type’ object is not subscriptable” in a function signature