Skip to content

Instantly share code, notes, and snippets.

@monk1337
Created October 13, 2017 12:21
Show Gist options
  • Save monk1337/ab2b00f8bfc376e6b1a08a24bd28c1e0 to your computer and use it in GitHub Desktop.
Save monk1337/ab2b00f8bfc376e6b1a08a24bd28c1e0 to your computer and use it in GitHub Desktop.
my_dict = {
'name1': 'abc', 'job1': 'xyz', 'pos1': 'tak', 'phone1': 12345,
'name2': 'pqr', 'job2': 'ftr', 'pos2': 'lkj', 'phone2': 27654,
'name3': 'swq', 'job3': 'nme', 'pos3': 'mnb', 'phone3': 98421,
'bad': 'bad_data',
}
group_dict=dict()
for key,value in my_dict.items():
if key[-1] not in group_dict:
group_dict[key[-1]]=[(key,value)]
else:
try:
group_dict[key[-1]].append((key,value))
except keyError:
pass
new_dict={}
def groups(x):
for key,value in x.items():
yield {x[0]: x[1] for x in value}
new=groups(group_dict)
print(new.__next__())
print(new.__next__())
print(new.__next__())
print(new.__next__())
print(list(groups(group_dict)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment