Reputation: 674
Below I have a list of dictionaries:
dict = [{'name': 'Sector',
'entity': 'ORG(100.0), nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan',
'synonyms': "Sector:['sector', 'sphere'], , ",
'definition': 'Sector: a plane figure bounded by two radii and the included arc of a circle',
'sentiment': ''},
{'name': 'Community Name',
'entity': 'PERSON(39.74), GPE(22.88), ORG(20.57), LOC(9.95), FAC(3.6), NORP(2.02), CARDINAL(0.45), LAW(0.39), DATE(0.39), nan, nan, nan, nan, nan',
'synonyms': "Community:['biotic_community', 'community', 'community_of_interests', 'residential_area', 'residential_district'], Name:['advert', 'appoint', 'bring_up', 'call', 'cite', 'constitute', 'describe', 'diagnose', 'discover', 'distinguish', 'epithet', 'figure', 'gens', 'identify', 'key', 'key_out', 'list', 'make', 'mention', 'name', 'nominate', 'public_figure', 'refer'], ",
'definition': 'Community: a group of people living in a particular local area, Name: a language unit by which a person or thing is known',
'sentiment': ''}]
How do I add a new key that groups entity, synonyms, definition, and sentiment as values?
desired output (nlp is the new added key):
dict = [{'name': 'Sector',
'nlp': {
'entity': 'ORG(100.0), nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan',
'synonyms': "Sector:['sector', 'sphere'], , ",
'definition': 'Sector: a plane figure bounded by two radii and the included arc of a circle',
'sentiment': ''}},
{'name': 'Community Name',
'nlp':{
'entity': 'PERSON(39.74), GPE(22.88), ORG(20.57), LOC(9.95), FAC(3.6), NORP(2.02), CARDINAL(0.45), LAW(0.39), DATE(0.39), nan, nan, nan, nan, nan',
'synonyms': "Community:['biotic_community', 'community', 'community_of_interests', 'residential_area', 'residential_district'], Name:['advert', 'appoint', 'bring_up', 'call', 'cite', 'constitute', 'describe', 'diagnose', 'discover', 'distinguish', 'epithet', 'figure', 'gens', 'identify', 'key', 'key_out', 'list', 'make', 'mention', 'name', 'nominate', 'public_figure', 'refer'], ",
'definition': 'Community: a group of people living in a particular local area, Name: a language unit by which a person or thing is known',
'sentiment': ''}}]
Upvotes: 0
Views: 49
Reputation: 2451
pop()
removes an element and returns that element
result = [{'name':d.pop('name'), 'nlp':d} for d in dict]
By using dictinary key name
we are removing that key from d
. As said, d.pop('name')
will return the value in d['name']
After d.pop('name')
, d
will not have the key name
in it.
Upvotes: 1
Reputation: 43189
You may use .pop()
in a for loop:
lst = [{'name': 'Sector',
'entity': 'ORG(100.0), nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan',
'synonyms': "Sector:['sector', 'sphere'], , ",
'definition': 'Sector: a plane figure bounded by two radii and the included arc of a circle',
'sentiment': ''},
{'name': 'Community Name',
'entity': 'PERSON(39.74), GPE(22.88), ORG(20.57), LOC(9.95), FAC(3.6), NORP(2.02), CARDINAL(0.45), LAW(0.39), DATE(0.39), nan, nan, nan, nan, nan',
'synonyms': "Community:['biotic_community', 'community', 'community_of_interests', 'residential_area', 'residential_district'], Name:['advert', 'appoint', 'bring_up', 'call', 'cite', 'constitute', 'describe', 'diagnose', 'discover', 'distinguish', 'epithet', 'figure', 'gens', 'identify', 'key', 'key_out', 'list', 'make', 'mention', 'name', 'nominate', 'public_figure', 'refer'], ",
'definition': 'Community: a group of people living in a particular local area, Name: a language unit by which a person or thing is known',
'sentiment': ''}]
result = []
for dct in lst:
newdict = {}
newdict["name"] = dct.pop('name')
newdict["nlp"] = dct
result.append(newdict)
print(result)
Which yields
[{'name': 'Sector', 'nlp': {'entity': 'ORG(100.0), nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan', 'synonyms': "Sector:['sector', 'sphere'], , ", 'definition': 'Sector: a plane figure bounded by two radii and the included arc of a circle', 'sentiment': ''}}, {'name': 'Community Name', 'nlp': {'entity': 'PERSON(39.74), GPE(22.88), ORG(20.57), LOC(9.95), FAC(3.6), NORP(2.02), CARDINAL(0.45), LAW(0.39), DATE(0.39), nan, nan, nan, nan, nan', 'synonyms': "Community:['biotic_community', 'community', 'community_of_interests', 'residential_area', 'residential_district'], Name:['advert', 'appoint', 'bring_up', 'call', 'cite', 'constitute', 'describe', 'diagnose', 'discover', 'distinguish', 'epithet', 'figure', 'gens', 'identify', 'key', 'key_out', 'list', 'make', 'mention', 'name', 'nominate', 'public_figure', 'refer'], ", 'definition': 'Community: a group of people living in a particular local area, Name: a language unit by which a person or thing is known', 'sentiment': ''}}]
Upvotes: 1