## How to map one list to another in python?

from collections import defaultdict

myDict = defaultdict(int)

for x in mylist:
myDict[x] += 1

Of course if you want the list inbetween result, just get the values from the dict (mydict.values()).

Note

## How to map one list to another in python?

dict(zip(['a','a','b','c','c','c'], [2, 2, 1, 3, 3, 3]))
Note

## How to map one list to another in python?

>>> x=['a','a','b','c','c','c']
>>> map(x.count,x)
[2, 2, 1, 3, 3, 3]
>>> dict(zip(x,map(x.count,x)))
{'a': 2, 'c': 3, 'b': 1}
>>>

But he never said anything about performance... this solves the problem, if it's too inefficient that's a different problem to solve.

Relax. You're most likely writing O(n^3) and O(2^n) algorithms all the time, and never even notice them being executed. Solve those problems if they happen.

That is right -- but did you ever realize what can happen, wenn you run into the O(n^2)-trap? I experienced it, when a simple algorithm killed program performance totally because it happened to be a bigger dataset and the algorithm had such behaviour.

The result looks pretty, but it has potential O(n^2) runtime behaviour.

Note

## How to map one list to another in python?

a = ['a','a','b','c','c','c']
b = [a.count(x) for x in a]
c = dict(zip(a, b))
c = dict(zip(a, b))
Note

## How to map one list to another in python?

>>> c = _
>>> [c[i] for i in l]   # or map(c.__getitem__, l)
[2, 2, 1, 3, 3, 3]
>>> l = ['a','a','b','c','c','c']
>>> Counter(l)
Counter({'c': 3, 'a': 2, 'b': 1})
[2, 2, 1, 3, 3, 3]

On Python 2.7 or 3.1, we have a built-in data structure collections.Counter to tally a list

Note