简体   繁体   中英

python map function with dictionaries in list

def card (Name,F_Name,Roll_No,Program):    

    x= f'''
    University Name
    
    
    Name: {Name}
    Father Name: {F_Name}
    Roll No: {Roll_No}
    Program: {Program}

    '''
    return x

data = [{'Name':'Student1','F_Name':"student's 1 father",'Roll_No':'123','Program':'BSCS'},{'Name':'student2','F_Name:':"student's 2 father",'Roll_No':'456','Program':'BSCS'},{'Name':'student3','F_Name':"student's 3 father",'Roll_No':'789','Program':'BSCS'}]

id_card = list(map(card,**data))

print (id_card)

I want to run this function using the map function and iterate the dictionary value in their respective positions in the user defined function.

map() can't spread the dictionaries itself. **data requires data to be a dictionary, not a list of dictionaries. Use a lambda to spread the dictionary during each call.

id_card = list(map(lambda d: card(**d), data))

or use a list comprehension:

id_card = [card(**d) for d in data]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM