3

Hello all python developers, I was playing with python lists and Pandas library and am having a trouble in a list manipulation tasks. I want to merge all test_list[i][0] dictionary items into one nested list index, according to same state name at index 0 of each nested list.

Sample Input:

test_list= [['Alabama', {'Baldwin County': 182265}],
 ['Alabama', {'Barbour County': 27457}],
 ['Arkansas', {'Newton County': 8330}],
 ['Arkansas', {'Perry County': 10445}],
 ['Arkansas', {'Phillips County': 21757}],
 ['California', {'Madera County': 150865}],
 ['California', {'Marin County': 252409}],
 ['Colorado', {'Adams County': 441603}],
 ['Colorado', {'Alamosa County': 15445}],
 ['Colorado', {'Arapahoe County': 572003}]
]

Sample Output:

test_list1= [['Alabama', {'Baldwin County': 182265, 'Barbour County': 27457}],
 ['Arkansas', {'Newton County': 8330, 'Perry County': 10445, 'Phillips County': 21757}],
 ['California', {'Madera County': 150865, 'Marin County': 252409}],
 ['Colorado', {'Adams County': 441603, 'Alamosa County': 15445, 'Arapahoe County': 572003}]
]

I have tried many approaches to solve this but no success so far.I am a beginner python developer. Thanks for helping in advance.

2
  • second sample input you mean expected output? that you want to make your list looke like? Commented Oct 30, 2019 at 7:04
  • Yes!! Sample output (edited) Commented Oct 30, 2019 at 7:06

2 Answers 2

3

Approach

  • Use collections.defaultdict as a way to group data by a common field (in this case, by state).

  • For each state, the defaultdict creates a new dict which is updated with the dict.update() method.

  • Turn the result back into a list with list applied to the dictionary's items (key/value pairs).

Working code

>>> from pprint import pprint
>>> from collections import defaultdict
>>> d = defaultdict(dict)
>>> for state, info in test_list:
        d[state].update(info)

>>> result = list(d.items())
>>> pprint(result)
[('Alabama', {'Baldwin County': 182265, 'Barbour County': 27457}),
 ('Arkansas',
  {'Newton County': 8330, 'Perry County': 10445, 'Phillips County': 21757}),
 ('California', {'Madera County': 150865, 'Marin County': 252409}),
 ('Colorado',
  {'Adams County': 441603, 'Alamosa County': 15445, 'Arapahoe County': 572003})]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for quick respond @raymond-hettinger. Your solution worked efficiently . :)
If list of lists needed (instead of list of tuples) then result could be written as [[*pair] for pair in d.items()]
2

Python3.5 or later

tmp_dict = {}
for lst in test_list:
city = lst[0]
country = lst[1]
if city in tmp_dict:
    tmp_dict[city] = {**tmp_dict[city], **country}
else:
    tmp_dict[city] = country
print(tmp_dict) #you will get dict result
#If you want to get list only
output_result = []
for k in tmp_dict:
    tmp_list.append([k,tmp_dict[k]])
print(output_result) #you will get list result

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.