0

I have a dictionary that looks like :

dict={'name':'ed|tom|pete','surname':'abc|def'}

How do I convert the keys with the delimeter | into a list ? Something that looks like:

dict1={'name':['ed','tom','pete'],'surname':['abc','def']}

thanks...

Number Logic
  • 732
  • 1
  • 7
  • 17

2 Answers2

1

You can iterate through each keys and split it on the delimeter

for key in dict1.keys():
    dict1[key] = dict1[key].split('|')

Sample Input:

>>dict1={'name':'ed|tom|pete','surname':'abc|def'}

Sample Output:

>>dict1
{'name': ['ed', 'tom', 'pete'], 'surname': ['abc', 'def']}
ThePyGuy
  • 13,387
  • 4
  • 15
  • 42
1

Use a dict-comprehension along with str.split

values = {'name': 'ed|tom|pete', 'surname': 'abc|def'}
values = {k: v.split("|") for k, v in values.items()}
azro
  • 47,041
  • 7
  • 30
  • 65