Title: Using map and lambda to count frequency in a dictionary
Tags: python;dictionary;lambda
Question: So the task is rather simple. Read in a string and store each character and its frequency in a dictionary then return the dictionary. I did it rather easily with a for loop.

```def getInputFreq():
    txt = input('Enter a string value: ')
    d = dict()
    for c in txt:
           d[c] = d.get(c,0) + 1
    return d
```

The issue is that I need to rewrite this statement using a map and lambda. 
I've tried a few things, early attempts returned empty dictionaries ( code has been lost in the attempts ). 

My latest attempt was ( in place of the for loop in above )

``` d = map((lambda x: (d.get(x,0)+1)),txt)
```

which returns a map object address.

Any suggestions?

Comment: This is an odd requirement, in my opinion. `map` returns an iterable with as many elements as the iterable you pass it. But your dictionary will usually be far smaller than your `txt` string. So it's very awkward to try to get a dictionary out of `map`'s return value.

Comment: Why not use `collections.Counter()`?

Comment: ok, try `d = list(map((lambda x: (d.get(x,0)+1)),txt))`. But that won't work

Comment: Yea, this is part of a bigger project but the map and lambda constraint was thrown in there.

Comment: @Jean-FrançoisFabre, This returns a list of 1s.

Comment: –1 for arbitrary restrictions

Comment: Why the `map` and `lambda` constraint, I assume it's for school?

Here is the accepted answer: First, in python 3, you have to force list iteration on ```map```

Then, your approach won't work, you'll get all ones or zeroes, because the expression doesn't accumulate the counts.

You could use ```str.count``` in a lambda, and map the tuples to a dictionary, that works:

```txt = "hello"

d = dict(map(lambda x : (x, txt.count(x)), set(txt)))
```

result:

```{'e': 1, 'l': 2, 'h': 1, 'o': 1}
```

But once again, ```collections.Counter``` is the preferred way to do that.

Comment for this answer: Even if the input contains no duplicate letters, this is still inefficient. `.count` is O(N), and you're calling it N times, for a total of O(N^2). By comparison, `Counter` is O(N). But still, this is a fairly concise way to satisfy OP's strange requirements, so +1.
Comment for this answer: how about `list(map(lambda x: x, [])) or dict(Counter('hello'))`, meeting the requirements to use map and lambda ;)
Comment for this answer: counting duplicate letters only once can also be achieved by casting to  `set` first. Like: `d = dict(map(lambda x : (x, txt.count(x)), set(txt)))`
Comment for this answer: @Chris_Rands That's hustling.. xD


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 7, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:11/20/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><dictionary><lambda>', 'Title': 'Using map and lambda to count frequency in a dictionary', 'ViewCount': 2828, 'id': 47395676, 'user_AboutMe': None, 'user_AccountId': 10816535.0, 'user_DisplayName': 'better_mixmaster', 'user_Reputation': 69.0, 'user_Views': 3.0, 'user_id': 7955280.0}}