TypeError: 'dict' object is not callable

PythonDictionary

Python Problem Overview


I'm trying to loop over elements of an input string, and get them from a dictionary. What am I doing wrong?

number_map = { 1: -3, 2: -2, 3: -1, 4: 1, 5: 2, 6: 3 }
input_str = raw_input("Enter something: ")
strikes = [number_map(int(x)) for x in input_str.split()]

strikes  = [number_map(int(x)) for x in input_str.split()]
TypeError: 'dict' object is not callable

Python Solutions


Solution 1 - Python

The syntax for accessing a dict given a key is number_map[int(x)]. number_map(int(x)) would actually be a function call but since number_map is not a callable, an exception is raised.

Solution 2 - Python

Access the dictionary with square brackets.

strikes = [number_map[int(x)] for x in input_str.split()]

Solution 3 - Python

You need to use [] to access elements of a dictionary. Not ()

  number_map = { 1: -3, 2: -2, 3: -1, 4: 1, 5: 2, 6: 3 }
input_str = raw_input("Enter something: ")
strikes = [number_map[int(x)] for x in input_str ]

Solution 4 - Python

strikes  = [number_map[int(x)] for x in input_str.split()]

You get an element from a dict using these [] brackets, not these ().

Solution 5 - Python

strikes  = [number_map[int(x)] for x in input_str.split()]

Use square brackets to explore dictionaries.

Solution 6 - Python

You need to use:

number_map[int(x)]

Note the square brackets!

Solution 7 - Python

A more functional approach would be by using dict.get

input_nums = [int(in_str) for in_str in input_str.split())
strikes = list(map(number_map.get, input_nums.split()))

One can observe that the conversion is a little clumsy, better would be to use the abstraction of function composition:

def compose2(f, g):
    return lambda x: f(g(x))

strikes = list(map(compose2(number_map.get, int), input_str.split()))

Example:

list(map(compose2(number_map.get, int), ["1", "2", "7"]))
Out[29]: [-3, -2, None]

Obviously in Python 3 you would avoid the explicit conversion to a list. A more general approach for function composition in Python can be found here.

(Remark: I came here from the Design of Computer Programs Udacity class, to write:)

def word_score(word):
    "The sum of the individual letter point scores for this word."
    return sum(map(POINTS.get, word))

Solution 8 - Python

it's number_map[int(x)], you tried to actually call the map with one argument

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEnchanterkeibyView Question on Stackoverflow
Solution 1 - PythonjenaView Answer on Stackoverflow
Solution 2 - PythonW. Kevin HazzardView Answer on Stackoverflow
Solution 3 - Pythonuser5536008View Answer on Stackoverflow
Solution 4 - PythonOleh PrypinView Answer on Stackoverflow
Solution 5 - PythonSebastiano MerlinoView Answer on Stackoverflow
Solution 6 - PythondetlyView Answer on Stackoverflow
Solution 7 - PythonuberwachView Answer on Stackoverflow
Solution 8 - PythonKaroly HorvathView Answer on Stackoverflow