0

I have this list of dict

{"FieldFlags": "0", "FieldNameAlt": "Please forgive me", "FieldName": "field1", "FieldType": "Text", "FieldJustification": "Left", "FieldValue": "test"},
 {"FieldJustification": "Left", "FieldNameAlt": "Please forgive me", "FieldName": "min_speed", "FieldType": "Text", "FieldFlags": "0"},
 {"FieldJustification": "Left", "FieldNameAlt": "Please forgive me", "FieldName": "avg_speed", "FieldType": "Text", "FieldFlags": "0"}, 
 {"FieldJustification": "Left", "FieldNameAlt": "Please forgive me", "FieldName": "lowest_speed", "FieldType": "Text", "FieldFlags": "0"},
 {"FieldJustification": "Left", "FieldNameAlt": "Please forgive me", "FieldName": "air", "FieldType": "Text", "FieldFlags": "0"}, 
 {"FieldJustification": "Left", "FieldNameAlt": "Please forgive me", "FieldName": "slope", "FieldType": "Text", "FieldFlags": "0"}]

For my purpose i need to convert this array to something like

fields = [('field1','test'),('min_speed',''),('avg_speed','')..]

So basically i want the tuple of (FiedName,FieldValue)

If fieldValue is not there , then it should be displayed as empty

How can i convert that

2 Answers 2

4
fields = [(d['FieldName'], d.get('FieldValue', '')) for d in your_list]
Sign up to request clarification or add additional context in comments.

1 Comment

d is just a name for the values in your_list while iterating over them. It could have been anything, but I chose d as we're iterating over a list of d ictionaries.
0

You can also use map():

fields = map(lambda d: (d['FieldName'], d.get('FieldValue', '')), your_list)

Comments

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.