Skip to content

Instantly share code, notes, and snippets.

@kerenskybr
Created April 20, 2021 17:11
Show Gist options
  • Save kerenskybr/0683adf4f8b17f13fe745577a1f87caa to your computer and use it in GitHub Desktop.
Save kerenskybr/0683adf4f8b17f13fe745577a1f87caa to your computer and use it in GitHub Desktop.
test_list = [{'gfg' : [1, 5, 6, 7], 'good' : [9, 6, 2, 10], 'CS' : [4, 5, 6]},
{'gfg' : [5, 6, 7, 8], 'CS' : [5, 7, 10]},
{'gfg' : [7, 5], 'best' : [5, 7]}]
# Concatenate Similar Key values
# Using loop
res = dict()
for dict in test_list:
for list in dict:
if list in res:
res[list] += (dict[list])
else:
res[list] = dict[list]
#The original list is : [{'CS': [4, 5, 6], 'good': [9, 6, 2, 10], 'gfg': [1, 5, 6, 7]}, {'CS': [5, 7, 10], 'gfg': [5, 6, 7, 8]}, {'best': [5, 7], 'gfg': [7, 5]}]
#The concatenated dictionary : {'gfg': [1, 5, 6, 7, 5, 6, 7, 8, 7, 5], 'good': [9, 6, 2, 10], 'best': [5, 7], 'CS': [4, 5, 6, 5, 7, 10]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment