Reputation: 33
I want to get the title, first name and last name from a nested dictionary with 4 sections using the map lambda function
I have tried:
fullnames_employees = list(map(lambda x: x["title", "firstname", "lastname"], employees))
fullnames_employees = list(map(lambda x: x["title"+"firstname"+"lastname"], employees))
This is the dictionary of employees
{
"email": "[email protected]",
"employee_id": 101,
"firstname": "Jonathan",
"lastname": "Calderon",
"title": "Mr",
"work_phone": "(02) 3691 5845"
},
{
"email": "[email protected]",
"employee_id": 102,
"firstname": "Christopher",
"lastname": "Hansen",
"title": "Mr",
"work_phone": "(02) 5807 8580"
},
{
"email": "[email protected]",
"employee_id": 103,
"firstname": "Isabella",
"lastname": "Dorsey",
"title": "Mrs",
"work_phone": "(02) 6375 1060"
},
{
"email": "[email protected]",
"employee_id": 104,
"firstname": "Barbara",
"lastname": "Baker",
"title": "Ms",
"work_phone": "(03) 5729 4873"
}
]
I expect the results to be
Mr Jonathan Calderon
etc
Upvotes: 1
Views: 1597
Reputation: 11238
your lambda function was right to an extent but the way you get the data from the iterate element is wrong. You can't access the list that way, just get the each element data 1 by one and add them to the string and return it.
input data
employees = [
{
"email": "[email protected]",
"employee_id": 101,
"firstname": "Jonathan",
"lastname": "Calderon",
"title": "Mr",
"work_phone": "(02) 3691 5845"
},
{
"email": "[email protected]",
"employee_id": 102,
"firstname": "Christopher",
"lastname": "Hansen",
"title": "Mr",
"work_phone": "(02) 5807 8580"
},
{
"email": "[email protected]",
"employee_id": 103,
"firstname": "Isabella",
"lastname": "Dorsey",
"title": "Mrs",
"work_phone": "(02) 6375 1060"
},
{
"email": "[email protected]",
"employee_id": 104,
"firstname": "Barbara",
"lastname": "Baker",
"title": "Ms",
"work_phone": "(03) 5729 4873"
}
]
code
fullnames_employees = list(map(lambda x: '{} {} {}'.format(x["title"],x["firstname"],x["lastname"]), employees))
output
['Mr Jonathan Calderon',
'Mr Christopher Hansen',
'Mrs Isabella Dorsey',
'Ms Barbara Baker']
Upvotes: 2
Reputation: 1510
Except lambda I used simple for loop:
for i in employees:
print(i["title"]+ " " + i["firstname"] + " " + i["lastname"] )
result:
Mr Jonathan Calderon
Mr Christopher Hansen
Mrs Isabella Dorsey
Ms Barbara Baker
or simple list comprehension:
print([i["title"]+ " " + i["firstname"] + " " + i["lastname"] for i in employees])
Gives:
['Mr Jonathan Calderon', 'Mr Christopher Hansen', 'Mrs Isabella Dorsey', 'Ms Barbara Baker']
Upvotes: 0
Reputation: 19885
You cannot access multiple elements of dictionaries
like x["title", "firstname", "lastname"]
; you need to do it once per element.
After extracting the desired elements, you can join them with ' '.join
:
def extract(person):
return ' '.join(person[key] for key in ['title', 'firstname', 'lastname'])
fullnames_employees = [extract(person) for person in employees]
print(fullnames_employees)
Output:
['Mr Jonathan Calderon', 'Mr Christopher Hansen', 'Mrs Isabella Dorsey', 'Ms Barbara Baker']
Upvotes: 1