Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python get only values in each dictionary

I intend to get the values for each dictionary in array list and put the values in new dictionary.

There are two key,two values in each dictionary.

This is my array_list.

[{'Name': 'email', 
'Value': '[email protected]'}, 
{'Name': 'name', 
'Value': 'tester'}, 
{'Name': 'address', 
'Value': 'abc'}]

My expected outcome (to get the both values in each dictionary):

{'email': '[email protected]',
 'name': 'tester', 
'address': 'abc'}

My current code:

outcome = {}
x = ""

for i in range(len(array_list)):
    for key,value in array_list[i].items():
        if key == 'Value':
            x = value
        elif key == 'Name': 
            outcome[value] = x

I still not able to get the expected outcome. Any helps?

like image 527
JohnnyCc Avatar asked Jun 29 '26 18:06

JohnnyCc


1 Answers

l = [{'Name': 'email', 
'Value': '[email protected]'}, 
{'Name': 'name', 
'Value': 'tester'}, 
{'Name': 'address', 
'Value': 'abc'}]

{k['Name'] : k['Value'] for k in l}

the result is

{'address': 'abc', 'email': '[email protected]', 'name': 'tester'}
like image 100
caverac Avatar answered Jul 01 '26 06:07

caverac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!