mat5 = np.array([['a', 'g', 'c'], ['a', 'b', 'c'], ['f', 'c', 'c']]) val, cnts = np.unique(mat5, return_counts=True) dict_unique = {k: v for k, v in zip(val, cnts)} """ result: {'a': 2, 'b': 1, 'c': 4, 'f': 1, 'g': 1} """