I have dictionary which contain list of dictionaries as below.
I want to swap all values of list of dictionary based on name.
Example: swap_function('Arvind','Jayesh')
should swap other values like surname, fullname & email.
I have already tried a lot from other website's references but not able achieve my goal.
data = {
"items":[
{
"name":"Arvind",
"surname":"Patel",
"fullname":"Arvind Patel",
"email":"abc@xyx.com"
},
{
"name":"Jayesh",
"surname":"Patel",
"fullname":"Jayesh Patel",
"email":"Patel@gmail.com"
},
{
"name":"Krishna",
"surname":"dave",
"fullname":"Krishna dave",
"email":"Krishna@xyz.com"
},
{
"name":"Aditya",
"surname":"Patel",
"fullname":"Aditya Patel",
"email":"Aditya@abc.com"
}
]
}
I have tried like below but after that I am out of ideas.
def name_swap(name1, name2):
for key, item in data.items():
first_dict = item[0]
second_dict = item[1]
third_dict = item[2]
forth_dict = item[3]
fifth_dict = item[4]
after name_swap('Arvind', 'Krishna')
output :
data = {
"items":[
{
"name":"Arvind",
"surname":"dave",
"fullname":"Krishna dave",
"email":"Krishna@xyz.com"
},
{
"name":"Jayesh",
"surname":"Patel",
"fullname":"Jayesh Patel",
"email":"Patel@gmail.com"
},
{
"name":"Krishna",
"surname":"Patel",
"fullname":"Arvind Patel",
"email":"abc@xyx.com"
},
{
"name":"Aditya",
"surname":"Patel",
"fullname":"Aditya Patel",
"email":"Aditya@abc.com"
}
]
}