0

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
sayth
  • 6,696
  • 12
  • 58
  • 100

3 Answers3

1

Why not just enumerate on the list and extract the values:

for i, entry in enumerate(dicts):
...             print entry['name']," ",entry['age']

And then the values can be written to the csv file

with open('dict.csv', 'wb') as csv_file:
...         writer = csv.writer(csv_file)
...         for i, entry in enumerate(dicts):
...             writer.writerow([entry['name'],entry['age']])
Rudrani Angira
  • 956
  • 2
  • 14
  • 28
  • 1
    Because I use python3 your eumerate is good but I got it working using for i, entry in enumerate(dicts): print("{0}, {1}".format(entry['name'], entry['age'])) – sayth Aug 30 '17 at 23:19
1

You can re-construct the dict to meet your needs:

dicts = [{'key': k, 'value': v} for item in dicts for k,v in item.items()]

for item in dicts:
    print("Key: {key} and Value: {value}".format(**item))

But, to write them directly to csv, there is a better way:

import csv

dicts = [{'key': k, 'value': v} for item in dicts for k,v in item.items()]

with open('dicts.csv', 'wb') as f:
    dict_writer = csv.DictWriter(f, dicts[0].keys())
    dict_writer.writeheader()
    dict_writer.writerows(dicts)
Nuno André
  • 4,739
  • 1
  • 33
  • 46
0

You can convert a list of same-keys dicts into a dict with the same keys and values are lists of the original values in original order:

dicts = [
    {"name": "Tom", "age": 10},
    {"name": "Pam", "age": 7},
    {"name": "Dick", "age": 12},
]

dictoflists = {k: [d[k] for d in dicts] for k in dicts[0].keys()}
Doctor Core
  • 102
  • 5