0

I have this array of arrays in python.

staff = [
            ['staff1', '2/18/1998', 21.63],
            ['staff2', '7/7/1999',  15.87],
            ['staff3', '8/26/2004', 123.46],
        ]

I have this array which specify the columns.

out_columns = ['Name','Date','Profit']

Based on these 2 arrays, I would like to convert them into an array of dictionaries that look like this;

dict_arr = [
                {"Name":"staff1",
                 "Date":"2/18/1998",
                 "Profit":21.63
                 },
                {"Name": "staff2",
                 "Date": "7/7/1999",
                 "Profit": 15.87
                 },
                {"Name": "staff3",
                 "Date": "8/26/2004",
                 "Profit": 123.46
                 },
]

I am using python v3.6

1
  • SO is not a free coding site. Please demonstrate effort to solve yourself. Commented Aug 10, 2018 at 4:26

1 Answer 1

4

Use zip function :

nl = []
for zz in staff:
    aa = {}
    for key,val in zip(out_columns,zz):
        aa[key]= val
    nl.append( aa )
print(nl) # [{'Name': 'staff1', 'Date': '2/18/1998', 'Profit': 21.63} , ... ]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for your help. I was at a loss over how to even start. I will study your answer carefully.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.