Get value from multiple dictionaries with same key

Viewed 97

I want to get the value of Name and value of Salary from the given code below.

    {
        "records": [
            {
                "fieldName": "Name",
                "value": "Alex"
            },
            {
                "fieldName": "Age",
                "value": "22"
            },
            {
                "fieldName": "Salary",
                "value": "235000.0"
            },
            {
                "fieldName": "Entry.Date",
                "value": "Sat Jan 02 00:00:00 GMT 2021"
            }]
        }
4 Answers

Parse the whole thing first:

out = {o['fieldName']: o['value'] for o in obj['records']}                                                                                                                                                                                 

Output:

{'Name': 'Alex',
 'Age': '22',
 'Salary': '235000.0',
 'Entry.Date': 'Sat Jan 02 00:00:00 GMT 2021'}

Then grab what you want:

name, salary = out['Name'], out['Salary']                                                                                           

I would suggest to use a simple for loop based approach instead since list comprehension requires multiple iterations over the list:

data = {
        "records": [
            {
                "fieldName": "Name",
                "value": "Alex"
            },
            {
                "fieldName": "Age",
                "value": "22"
            },
            {
                "fieldName": "Salary",
                "value": "235000.0"
            },
            {
                "fieldName": "Entry.Date",
                "value": "Sat Jan 02 00:00:00 GMT 2021"
            }]
        }


name, salary = None, None
for record in data["records"]:
    if record.get("fieldName") == "Name":
        name = record.get("value")
    
    if record.get("fieldName") == "Salary":
        salary = record.get("value")
    
print(name, salary)

Output:

Alex 235000.0

This assumes that the fields appear exactly one time. If not, remove the colon to get a list with all values.


name, = [d['value'] for d in your_dictionary['records'] if d['fieldName']=='Name']
salary, = [d['value'] for d in your_dictionary['records'] if d['fieldName']=='Salary']

Use simple approach of Comprehension

dct = {
        "records": [
            {
                "fieldName": "Name",
                "value": "Alex"
            },
            {
                "fieldName": "Age",
                "value": "22"
            },
            {
                "fieldName": "Salary",
                "value": "235000.0"
            },
            {
                "fieldName": "Entry.Date",
                "value": "Sat Jan 02 00:00:00 GMT 2021"
            }]
    }
data = {name : salary for (name, salary) in zip([d['value'] for d in dct['records'] if d['fieldName'] == 'Name'], [d['value'] for d in dct['records'] if d['fieldName'] == 'Salary'])}
print(data)
Related