Search code examples
pythonlistdictionarysplitdelimiter

Replace dictionary CSV values with lists


X = {"continent": "ASIA, EUROPE, AFRICA" , "Countries" : "JAPAN, GERMANY, EGYPT" , "Capital" : "TOKYO, BERLIN, CAIRO"}   

The resulted dictionary should look like this

X = {"continent": ["ASIA", "EUROPE", "AFRICA"] , "Countries" : ["JAPAN", "GERMANY", "EGYPT"] , "Capital" : ["TOKYO", "BERLIN", "CAIRO"]}  

Separated each value of X inside the list.


Solution

  • You can use split function which returns a list after separating a string by a given character.

    for el in X:
      X[el] = X[el].split(', ')