I want to extract a list of dictionary values so I can write them to csv.
Using the info in this previous SO question I am attempting to replicate for all key, values.
In [41]: dicts = [
...: {"name": "Tom", "age": 10},
...: {"name": "Mark", "age": 5},
...: {"name": "Pam", "age": 7},
...: {"name": "Dick", "age": 12},
...: ]
However the output I am getting is very mixed, sometimes its a dict and sometimes a value.
In [25]: for item in dicts:
...: for k, v in item.items():
...: print("Key: {0} and Value: {1}".format(k,v))
...:
...:
Key: name and Value: {'name': 'Dick', 'age': 12, 0: {...}}
Key: age and Value: 10
Key: 0 and Value: {'name': 'Dick', 'age': 12, 0: {...}}
Key: name and Value: Mark
Key: age and Value: 5
Key: name and Value: Pam
Key: age and Value: 7
Key: name and Value: Dick
Key: age and Value: 12
Key: 0 and Value: {'name': 'Dick', 'age': 12, 0: {...}}
I want the output to have no dicts in it all keys and values extracted. There are better ways to do this I see but error was from pasting into ipython incorrectly.
Edited Updated Dicts Updated Output with updated Dicts does work as expected.
In [49]: for item in dicts:
...: for k, v in item.items():
...: print("Key: {0} and Value: {1}".format(k,v))
...:
Key: name and Value: Tom
Key: age and Value: 10
Key: name and Value: Mark
Key: age and Value: 5
Key: name and Value: Pam
Key: age and Value: 7
Key: name and Value: Dick
Key: age and Value: 12