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
  • 852
  • 1
  • 9
  • 19

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
  • 17,779
  • 5
  • 18
  • 45
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
  • 53,056
  • 7
  • 34
  • 70