Skip to content
Advertisement

How to extract the the key and values from list of dictionary?

How to extract the the key and values from list of dictionary?

Below is my data, i want to extract the key and values from list of dictionary.

data = [{'index': 0,
      'MaterialCode': '67567412',
      'DP_Category': 'HAIR CARE'},
      {'index': 1,
      'MaterialCode': '67567412',
      'DP_Category': 'HAIR CARE'}]

Advertisement

Answer

data = [
    {"index": 0, "MaterialCode": "67567412", "DP_Category": "HAIR CARE"},
    {"index": 1, "MaterialCode": "67567412", "DP_Category": "HAIR CARE"},
]

for idx, elem in enumerate(data):
    for key, value in elem.items():
        print(f"List element: {idx:>2} Key: {key:<20} Value: {value}")

output

List element:  0 Key: index                Value: 0
List element:  0 Key: MaterialCode         Value: 67567412
List element:  0 Key: DP_Category          Value: HAIR CARE
List element:  1 Key: index                Value: 1
List element:  1 Key: MaterialCode         Value: 67567412
List element:  1 Key: DP_Category          Value: HAIR CARE
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement