Search code examples
pythonlistdictionaryreturn

Trying to map through return list of dictionary with python


I want to do something pretty simple, but I don not know why it is not working

N = int(input())
numbers = list(input())
print(numbers)
def switch(x):
    return {
      '1': 2,
      '2': 5,
      '3': 5,
      '4': 4,
      '5': 5,
      '6': 6,
      '7': 3,
      '8': 7,
      '9': 6,
    }[x]

print(list(map(switch, numbers)))

I have the above code and what I want is to based in a number sum all of the results for each of its digits from a function called switch


Solution

  • Do you want to sum all digits? in that case you can use sum function:

    ...
    print(sum(map(switch, numbers)))
    

    You don't need to convert map result to list since sum accepts iterables.