-4

I've got a dictionary of unique id's (strings), and values for each of these id's (also strings). The problem is that some of the keys have multiple values separated by commas. I can't think of how to use string manipulation to split them based on commas and still have them assigned to their key.

for example...

'abcde': 'abc,def' 

and I would like it to be:

'abcde': 'abc'
'abcde': 'def'

any insight would be greatly appreciated

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
chuckset
  • 1
  • 1

1 Answers1

1

Why not just split?

>>> my_dict = {'key1':'apple,banana,cherry', 'key2':'date,fig', 'key3':'grape,honeydew'}
>>> new_dict = {k, v.split(',') for k, v in my_dict.items()}:
>>> new_dict
{'key1': ['apple', 'banana', 'cherry'],
 'key2': ['date', 'fig'],
 'key3': ['grape', 'honeydew']}
Sebastian Mendez
  • 2,859
  • 14
  • 25