Why when I try passing a dictionary as argument to map (), you receive the error?

a = {
 'title': 'course_title',
 'subtitle': 'course_subtitle',
 'id': 'course_id'
print(list(map(lambda obj: f"({obj['id']}) {obj['title']} - {obj['subtitle']}", a)))

Tried to substitute in the format string value from a dictionary, but got error
TypeError: string indices must be integers
What could be the problem?
April 4th 20 at 00:42
2 answers
April 4th 20 at 00:44
Because map takes a list as second argument, and cast the dict to a list is a list of the keys so that the obj in the lambda is string.

You can watch that will show these print to understand:

print (list(a))
print (list(map(lambda x: x, a)))

Solution: instead of obj['id'] to use in lambda-expression a[obj]['id']
April 4th 20 at 00:46
Everything is much simpler:
'({id}) {title} - {subtitle}'.format(**a)

Find more questions by tags Python