Python Forum

Full Version: using map function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am getting following error:

Traceback (most recent call last):
File "./map_function.py", line 13, in <module>
name = map(name_split(person1,name),people)
TypeError: 'list' object is not callable

def name_split(person,name):

    name = person.split(' ')

    return (name)

global name

name = []

people = ['Dr. Christopher Brooks', 'Dr. Kevyn Collins-Thompson', 'Dr. VG Vinod Vydiswaran', 'Dr. Daniel Romero']

for person1 in people:

    name = map(name_split(name), people)

print name
The first argument of map is the function which is called repeatedly with the elements of the iterable. Don't call the function when you pass it to a map function.

def name_split(name):
    return name.split()

people = ['Dr. Christopher Brooks', 'Dr. Kevyn Collins-Thompson', 'Dr. VG Vinod Vydiswaran', 'Dr. Daniel Romero']
splitted_names = list(map(name_split, people))
print(splitted_names)