2

I get as input a list with three items

[{item1},{item2},{item3}]

But each item takes the form (including the curley brackets):

{'key1': 'val1', 'key2': val2, 'key3': val3}

so when I print my list it looks like this (again the curley brackets are not set by me but are just delivered as you see it by input via API response):

[{'key1': 'val1', 'key2': val2, 'key3': val3}, {'key4': 'val4', 'key5': val5, 'key6': val6}, {'key7': 'val7', 'key8': val8, 'key9':val9}]

I want to have a dict where every comma separated key-val pair within each item is an element. hence:

{'key1': 'val1', key2': val2, 'key3': val3, 'key4': 'val4', 'key5': val5, 'key6': val6, 'key7': 'val7', 'key8': val8, 'key9':val9}

I tried first to make a new list which only contains "item1", hence newlist contains [{'key1': 'val1', 'key2': val2, 'key3': val3}] the length of this new list is said to be 3 items and I thought: perfect! Now I can access key1-val1 pair by newlist[0] and feed a dict with {newlist[0],newlist[1],newlist[2]} to get {'key1': 'val1', 'key2': val2, 'key3': val3}, but this does not work.

Any suggestion? I guess the curley brackets are the problem? I mean I found various answers googleing "list to dict conversion" but none of what I saw had that...

Philipp Maurer
  • 2,480
  • 6
  • 18
  • 25
richman2
  • 21
  • 1
  • 1
    is this what you are looking for : https://stackoverflow.com/questions/5236296/how-to-convert-list-of-dict-to-dict – Vikash Singh Jan 19 '18 at 14:12
  • So you are trying to turn a list of 2 maps into 1 map, correct? – Fallenreaper Jan 19 '18 at 14:15
  • _"the length of this new list is said to be 3 items"_. Strange, that's not what I get. On my machine, `len([{'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}])` gives me `1`. – Kevin Jan 19 '18 at 14:16
  • thank you, it helps but in this question it is always "name" which is the key, followed by several vals. Like [{key1: val1, val2, val3}, {key1: val4, val5, val6} But I have different keys so I cant run it by accessing 'name' (key1)... This is just a first comment, maybe after some thinking I can build something on it... as I am new, everything takes ages to understand and adopt :) – richman2 Jan 19 '18 at 14:28
  • It seems like newdict=dict([(key,d[key]) for d in data for key in d]) suggestion in one of the answers to the duplicate questions solved the problem. Though again (like in a answer here below) only the last list item3 from the original list of [{item1},{item2},{item3}} is contained in newdict (i.e. key7: val7, key8:val8, key9:val9) but I guess I can solve this. – richman2 Jan 19 '18 at 15:24

2 Answers2

0

You can flatten your list of dicts with a comprehension:

>>> d = [{'key1': 1, 'key2': 2, 'key3': 3}, {'key4': 4, 'key5': 5, 'key6': 6}, {'key7': 7, 'key8': 8, 'key9':9}]
>>> dict(x for y in d for x in y.items())
{'key1': 1, 'key2': 2, 'key3': 3, 'key4': 4, 'key5': 5, 'key6': 6, 'key7': 7, 'key8': 8, 'key9': 9}

Then you can easily access any of your keys:

>>> d['key1']
1
>>> d['key9']
9
RoadRunner
  • 25,803
  • 6
  • 42
  • 75
  • When I copy paste your code to a new file and let it run I get File "C:....", line 5, in d['key1'] TypeError: list indices must be integers or slices, not str, entering d[1] gives me {'key4': 4, 'key5': 5, 'key6': 6}, which is just the second "item" of the list but not broken down in key4: 4 NEW dict item key5: 5 etc... – richman2 Jan 19 '18 at 15:03
0

Check this post: How do I merge a list of dicts into a single dict?

What you are looking for is:

data = [{'key1': 'val1', 'key2': val2, 'key3': val3}, {'key4': 'val4', 'key5': val5, 'key6': val6}, {'key7': 'val7', 'key8': val8, 'key9':val9}]
res = {}
[ res.update(d) for d in data]
asabater
  • 94
  • 1
  • 9
  • I think you mean `for d in data: res.update(d)` List comprehensions create lists. They aren't there for making convenient one-liners. – Steven Rumbalski Jan 19 '18 at 14:28
  • I dont understand it fully, but it works! Though in res there is only the "last item" i.e. {key7 .... val9} contained not the first or second.. Anyway, I will elaborate on this! Thanks a lot for the quick answer! – richman2 Jan 19 '18 at 14:39