Conversion of single string to list of strings when new strings are added










3















I can't find an answer.
I read a dictionary from file, it has a string value for a key.
Then I check if a new value is already in the dictionary, if it isn't then I want
to append it to the previous string and to get a list of values for a given key.



For example if I have a dictionary



dict = "vehicles": "car", "animals": "cat"


and the following condition is true



if "dog" not in dict["animals"]:


I'd like to get the output



dict = "vehicles": "car", "animals": ["cat", "dog"] 









share|improve this question

















  • 6





    Heres a few tips to know. Dicts are unordered, meaning they don't store the positions of the keys. Also, using dict as a var name is a bad idea, since dict is the dict class.

    – Qwerty
    Nov 14 '18 at 15:02











  • @Qwerty, Is it true for json type in python?

    – James Flash
    Nov 14 '18 at 15:12











  • If you want order, I believe you are looking for an ordered dict, which I believe is in the collections library.

    – Qwerty
    Nov 14 '18 at 15:13












  • It is valuable remark, thanks

    – James Flash
    Nov 14 '18 at 15:20















3















I can't find an answer.
I read a dictionary from file, it has a string value for a key.
Then I check if a new value is already in the dictionary, if it isn't then I want
to append it to the previous string and to get a list of values for a given key.



For example if I have a dictionary



dict = "vehicles": "car", "animals": "cat"


and the following condition is true



if "dog" not in dict["animals"]:


I'd like to get the output



dict = "vehicles": "car", "animals": ["cat", "dog"] 









share|improve this question

















  • 6





    Heres a few tips to know. Dicts are unordered, meaning they don't store the positions of the keys. Also, using dict as a var name is a bad idea, since dict is the dict class.

    – Qwerty
    Nov 14 '18 at 15:02











  • @Qwerty, Is it true for json type in python?

    – James Flash
    Nov 14 '18 at 15:12











  • If you want order, I believe you are looking for an ordered dict, which I believe is in the collections library.

    – Qwerty
    Nov 14 '18 at 15:13












  • It is valuable remark, thanks

    – James Flash
    Nov 14 '18 at 15:20













3












3








3








I can't find an answer.
I read a dictionary from file, it has a string value for a key.
Then I check if a new value is already in the dictionary, if it isn't then I want
to append it to the previous string and to get a list of values for a given key.



For example if I have a dictionary



dict = "vehicles": "car", "animals": "cat"


and the following condition is true



if "dog" not in dict["animals"]:


I'd like to get the output



dict = "vehicles": "car", "animals": ["cat", "dog"] 









share|improve this question














I can't find an answer.
I read a dictionary from file, it has a string value for a key.
Then I check if a new value is already in the dictionary, if it isn't then I want
to append it to the previous string and to get a list of values for a given key.



For example if I have a dictionary



dict = "vehicles": "car", "animals": "cat"


and the following condition is true



if "dog" not in dict["animals"]:


I'd like to get the output



dict = "vehicles": "car", "animals": ["cat", "dog"] 






python string






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 14:58









James FlashJames Flash

406




406







  • 6





    Heres a few tips to know. Dicts are unordered, meaning they don't store the positions of the keys. Also, using dict as a var name is a bad idea, since dict is the dict class.

    – Qwerty
    Nov 14 '18 at 15:02











  • @Qwerty, Is it true for json type in python?

    – James Flash
    Nov 14 '18 at 15:12











  • If you want order, I believe you are looking for an ordered dict, which I believe is in the collections library.

    – Qwerty
    Nov 14 '18 at 15:13












  • It is valuable remark, thanks

    – James Flash
    Nov 14 '18 at 15:20












  • 6





    Heres a few tips to know. Dicts are unordered, meaning they don't store the positions of the keys. Also, using dict as a var name is a bad idea, since dict is the dict class.

    – Qwerty
    Nov 14 '18 at 15:02











  • @Qwerty, Is it true for json type in python?

    – James Flash
    Nov 14 '18 at 15:12











  • If you want order, I believe you are looking for an ordered dict, which I believe is in the collections library.

    – Qwerty
    Nov 14 '18 at 15:13












  • It is valuable remark, thanks

    – James Flash
    Nov 14 '18 at 15:20







6




6





Heres a few tips to know. Dicts are unordered, meaning they don't store the positions of the keys. Also, using dict as a var name is a bad idea, since dict is the dict class.

– Qwerty
Nov 14 '18 at 15:02





Heres a few tips to know. Dicts are unordered, meaning they don't store the positions of the keys. Also, using dict as a var name is a bad idea, since dict is the dict class.

– Qwerty
Nov 14 '18 at 15:02













@Qwerty, Is it true for json type in python?

– James Flash
Nov 14 '18 at 15:12





@Qwerty, Is it true for json type in python?

– James Flash
Nov 14 '18 at 15:12













If you want order, I believe you are looking for an ordered dict, which I believe is in the collections library.

– Qwerty
Nov 14 '18 at 15:13






If you want order, I believe you are looking for an ordered dict, which I believe is in the collections library.

– Qwerty
Nov 14 '18 at 15:13














It is valuable remark, thanks

– James Flash
Nov 14 '18 at 15:20





It is valuable remark, thanks

– James Flash
Nov 14 '18 at 15:20












5 Answers
5






active

oldest

votes


















1














Another way is to try append the element to the list and if it fails make the dictionary value a list:



dic = "vehicles": "car", "animals": "cat"
if "dog" not in dic["animals"]:
try:
dic["animals"].append("dog")
except:
dic["animals"] = [dic["animals"], "dog"]


Gives output:



'vehicles': 'car', 'animals': ['cat', 'dog']


In this way if you have a single element the except part will be executed, making the value a list of string. If you then try to add another element it will be appended to the list.






share|improve this answer

























  • Thanks, but for me It gives 'vehicles': 'car', 'animals': [['cat'], 'dog']

    – James Flash
    Nov 14 '18 at 15:23











  • Are you sure? It works properly for me, both in python 2 and 3...

    – toti08
    Nov 14 '18 at 15:31











  • Yeah it works, what is the difference from Jonathan R answer? Is it due to exception? Why does append() work with string here?

    – James Flash
    Nov 14 '18 at 15:35







  • 1





    Jonathan answer will produce nested lists in case you append more than one item. Here it will not. append will not work on strings, it will give you an exception, hence the try/except

    – toti08
    Nov 14 '18 at 15:41











  • It is a tricky way , thanks

    – James Flash
    Nov 14 '18 at 15:46


















4














start with a dictionary with values as set of strings



d = "vehicles": "car", "animals": "cat"


Then adding twice a new value only adds it once (and lookup is fast since it uses hashes):



d["animals"].add('dog')
d["animals"].add('dog')

>>> d
'animals': 'dog', 'cat', 'vehicles': 'car'


If you have the dictionary as input like in your question, you can transform it easily with values as set with dictionary comprehension like this:



loaded_d = "vehicles": "car", "animals": "cat" # dict just loaded from file
d = key:value for key,value in loaded_d.items()





share|improve this answer

























  • Ok, should I convert values to sets when I read a dictionary from a file?

    – James Flash
    Nov 14 '18 at 15:10






  • 1





    yes. I'll provide the way to do it, let me edit.

    – Jean-François Fabre
    Nov 14 '18 at 15:12


















1














>>> if "dog" not in dict["animals"]:
... dict["animals"] = [dict["animals"], "dog"]





share|improve this answer


















  • 3





    try to add a third animal that will nest your list once more.

    – Jean-François Fabre
    Nov 14 '18 at 15:03











  • that's not in the problem statement

    – Jonathan R
    Nov 14 '18 at 15:04











  • Thanks. Yeah it would be good to append more strings and get just a list of strings as @ Jean-François Fabre noticed

    – James Flash
    Nov 14 '18 at 15:06



















1














Should be the easies way I think



dict = "vehicles": "car", "animals": "cat"
if "dog" not in dict["animals"]:
dict["animals"].add("dog")





share|improve this answer
































    1














    You just need to update animals list by using append method.



    dict = "vehicles": "car", "animals": "cat"

    def appendItem(item):
    if item not in dict["animals"]:
    if not isinstance(dict["animals"], list):
    dict["animals"] = [dict["animals"]]
    dict["animals"].append(item)

    appendItem("dog")
    appendItem("dog")
    appendItem("rabbit")
    appendItem("cat")


    Output



    => 'vehicles': 'car', 'animals': ['cat', 'dog', 'rabbit']





    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303080%2fconversion-of-single-string-to-list-of-strings-when-new-strings-are-added%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Another way is to try append the element to the list and if it fails make the dictionary value a list:



      dic = "vehicles": "car", "animals": "cat"
      if "dog" not in dic["animals"]:
      try:
      dic["animals"].append("dog")
      except:
      dic["animals"] = [dic["animals"], "dog"]


      Gives output:



      'vehicles': 'car', 'animals': ['cat', 'dog']


      In this way if you have a single element the except part will be executed, making the value a list of string. If you then try to add another element it will be appended to the list.






      share|improve this answer

























      • Thanks, but for me It gives 'vehicles': 'car', 'animals': [['cat'], 'dog']

        – James Flash
        Nov 14 '18 at 15:23











      • Are you sure? It works properly for me, both in python 2 and 3...

        – toti08
        Nov 14 '18 at 15:31











      • Yeah it works, what is the difference from Jonathan R answer? Is it due to exception? Why does append() work with string here?

        – James Flash
        Nov 14 '18 at 15:35







      • 1





        Jonathan answer will produce nested lists in case you append more than one item. Here it will not. append will not work on strings, it will give you an exception, hence the try/except

        – toti08
        Nov 14 '18 at 15:41











      • It is a tricky way , thanks

        – James Flash
        Nov 14 '18 at 15:46















      1














      Another way is to try append the element to the list and if it fails make the dictionary value a list:



      dic = "vehicles": "car", "animals": "cat"
      if "dog" not in dic["animals"]:
      try:
      dic["animals"].append("dog")
      except:
      dic["animals"] = [dic["animals"], "dog"]


      Gives output:



      'vehicles': 'car', 'animals': ['cat', 'dog']


      In this way if you have a single element the except part will be executed, making the value a list of string. If you then try to add another element it will be appended to the list.






      share|improve this answer

























      • Thanks, but for me It gives 'vehicles': 'car', 'animals': [['cat'], 'dog']

        – James Flash
        Nov 14 '18 at 15:23











      • Are you sure? It works properly for me, both in python 2 and 3...

        – toti08
        Nov 14 '18 at 15:31











      • Yeah it works, what is the difference from Jonathan R answer? Is it due to exception? Why does append() work with string here?

        – James Flash
        Nov 14 '18 at 15:35







      • 1





        Jonathan answer will produce nested lists in case you append more than one item. Here it will not. append will not work on strings, it will give you an exception, hence the try/except

        – toti08
        Nov 14 '18 at 15:41











      • It is a tricky way , thanks

        – James Flash
        Nov 14 '18 at 15:46













      1












      1








      1







      Another way is to try append the element to the list and if it fails make the dictionary value a list:



      dic = "vehicles": "car", "animals": "cat"
      if "dog" not in dic["animals"]:
      try:
      dic["animals"].append("dog")
      except:
      dic["animals"] = [dic["animals"], "dog"]


      Gives output:



      'vehicles': 'car', 'animals': ['cat', 'dog']


      In this way if you have a single element the except part will be executed, making the value a list of string. If you then try to add another element it will be appended to the list.






      share|improve this answer















      Another way is to try append the element to the list and if it fails make the dictionary value a list:



      dic = "vehicles": "car", "animals": "cat"
      if "dog" not in dic["animals"]:
      try:
      dic["animals"].append("dog")
      except:
      dic["animals"] = [dic["animals"], "dog"]


      Gives output:



      'vehicles': 'car', 'animals': ['cat', 'dog']


      In this way if you have a single element the except part will be executed, making the value a list of string. If you then try to add another element it will be appended to the list.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 14 '18 at 15:15

























      answered Nov 14 '18 at 15:05









      toti08toti08

      1,78941623




      1,78941623












      • Thanks, but for me It gives 'vehicles': 'car', 'animals': [['cat'], 'dog']

        – James Flash
        Nov 14 '18 at 15:23











      • Are you sure? It works properly for me, both in python 2 and 3...

        – toti08
        Nov 14 '18 at 15:31











      • Yeah it works, what is the difference from Jonathan R answer? Is it due to exception? Why does append() work with string here?

        – James Flash
        Nov 14 '18 at 15:35







      • 1





        Jonathan answer will produce nested lists in case you append more than one item. Here it will not. append will not work on strings, it will give you an exception, hence the try/except

        – toti08
        Nov 14 '18 at 15:41











      • It is a tricky way , thanks

        – James Flash
        Nov 14 '18 at 15:46

















      • Thanks, but for me It gives 'vehicles': 'car', 'animals': [['cat'], 'dog']

        – James Flash
        Nov 14 '18 at 15:23











      • Are you sure? It works properly for me, both in python 2 and 3...

        – toti08
        Nov 14 '18 at 15:31











      • Yeah it works, what is the difference from Jonathan R answer? Is it due to exception? Why does append() work with string here?

        – James Flash
        Nov 14 '18 at 15:35







      • 1





        Jonathan answer will produce nested lists in case you append more than one item. Here it will not. append will not work on strings, it will give you an exception, hence the try/except

        – toti08
        Nov 14 '18 at 15:41











      • It is a tricky way , thanks

        – James Flash
        Nov 14 '18 at 15:46
















      Thanks, but for me It gives 'vehicles': 'car', 'animals': [['cat'], 'dog']

      – James Flash
      Nov 14 '18 at 15:23





      Thanks, but for me It gives 'vehicles': 'car', 'animals': [['cat'], 'dog']

      – James Flash
      Nov 14 '18 at 15:23













      Are you sure? It works properly for me, both in python 2 and 3...

      – toti08
      Nov 14 '18 at 15:31





      Are you sure? It works properly for me, both in python 2 and 3...

      – toti08
      Nov 14 '18 at 15:31













      Yeah it works, what is the difference from Jonathan R answer? Is it due to exception? Why does append() work with string here?

      – James Flash
      Nov 14 '18 at 15:35






      Yeah it works, what is the difference from Jonathan R answer? Is it due to exception? Why does append() work with string here?

      – James Flash
      Nov 14 '18 at 15:35





      1




      1





      Jonathan answer will produce nested lists in case you append more than one item. Here it will not. append will not work on strings, it will give you an exception, hence the try/except

      – toti08
      Nov 14 '18 at 15:41





      Jonathan answer will produce nested lists in case you append more than one item. Here it will not. append will not work on strings, it will give you an exception, hence the try/except

      – toti08
      Nov 14 '18 at 15:41













      It is a tricky way , thanks

      – James Flash
      Nov 14 '18 at 15:46





      It is a tricky way , thanks

      – James Flash
      Nov 14 '18 at 15:46













      4














      start with a dictionary with values as set of strings



      d = "vehicles": "car", "animals": "cat"


      Then adding twice a new value only adds it once (and lookup is fast since it uses hashes):



      d["animals"].add('dog')
      d["animals"].add('dog')

      >>> d
      'animals': 'dog', 'cat', 'vehicles': 'car'


      If you have the dictionary as input like in your question, you can transform it easily with values as set with dictionary comprehension like this:



      loaded_d = "vehicles": "car", "animals": "cat" # dict just loaded from file
      d = key:value for key,value in loaded_d.items()





      share|improve this answer

























      • Ok, should I convert values to sets when I read a dictionary from a file?

        – James Flash
        Nov 14 '18 at 15:10






      • 1





        yes. I'll provide the way to do it, let me edit.

        – Jean-François Fabre
        Nov 14 '18 at 15:12















      4














      start with a dictionary with values as set of strings



      d = "vehicles": "car", "animals": "cat"


      Then adding twice a new value only adds it once (and lookup is fast since it uses hashes):



      d["animals"].add('dog')
      d["animals"].add('dog')

      >>> d
      'animals': 'dog', 'cat', 'vehicles': 'car'


      If you have the dictionary as input like in your question, you can transform it easily with values as set with dictionary comprehension like this:



      loaded_d = "vehicles": "car", "animals": "cat" # dict just loaded from file
      d = key:value for key,value in loaded_d.items()





      share|improve this answer

























      • Ok, should I convert values to sets when I read a dictionary from a file?

        – James Flash
        Nov 14 '18 at 15:10






      • 1





        yes. I'll provide the way to do it, let me edit.

        – Jean-François Fabre
        Nov 14 '18 at 15:12













      4












      4








      4







      start with a dictionary with values as set of strings



      d = "vehicles": "car", "animals": "cat"


      Then adding twice a new value only adds it once (and lookup is fast since it uses hashes):



      d["animals"].add('dog')
      d["animals"].add('dog')

      >>> d
      'animals': 'dog', 'cat', 'vehicles': 'car'


      If you have the dictionary as input like in your question, you can transform it easily with values as set with dictionary comprehension like this:



      loaded_d = "vehicles": "car", "animals": "cat" # dict just loaded from file
      d = key:value for key,value in loaded_d.items()





      share|improve this answer















      start with a dictionary with values as set of strings



      d = "vehicles": "car", "animals": "cat"


      Then adding twice a new value only adds it once (and lookup is fast since it uses hashes):



      d["animals"].add('dog')
      d["animals"].add('dog')

      >>> d
      'animals': 'dog', 'cat', 'vehicles': 'car'


      If you have the dictionary as input like in your question, you can transform it easily with values as set with dictionary comprehension like this:



      loaded_d = "vehicles": "car", "animals": "cat" # dict just loaded from file
      d = key:value for key,value in loaded_d.items()






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 14 '18 at 15:14

























      answered Nov 14 '18 at 15:02









      Jean-François FabreJean-François Fabre

      106k956115




      106k956115












      • Ok, should I convert values to sets when I read a dictionary from a file?

        – James Flash
        Nov 14 '18 at 15:10






      • 1





        yes. I'll provide the way to do it, let me edit.

        – Jean-François Fabre
        Nov 14 '18 at 15:12

















      • Ok, should I convert values to sets when I read a dictionary from a file?

        – James Flash
        Nov 14 '18 at 15:10






      • 1





        yes. I'll provide the way to do it, let me edit.

        – Jean-François Fabre
        Nov 14 '18 at 15:12
















      Ok, should I convert values to sets when I read a dictionary from a file?

      – James Flash
      Nov 14 '18 at 15:10





      Ok, should I convert values to sets when I read a dictionary from a file?

      – James Flash
      Nov 14 '18 at 15:10




      1




      1





      yes. I'll provide the way to do it, let me edit.

      – Jean-François Fabre
      Nov 14 '18 at 15:12





      yes. I'll provide the way to do it, let me edit.

      – Jean-François Fabre
      Nov 14 '18 at 15:12











      1














      >>> if "dog" not in dict["animals"]:
      ... dict["animals"] = [dict["animals"], "dog"]





      share|improve this answer


















      • 3





        try to add a third animal that will nest your list once more.

        – Jean-François Fabre
        Nov 14 '18 at 15:03











      • that's not in the problem statement

        – Jonathan R
        Nov 14 '18 at 15:04











      • Thanks. Yeah it would be good to append more strings and get just a list of strings as @ Jean-François Fabre noticed

        – James Flash
        Nov 14 '18 at 15:06
















      1














      >>> if "dog" not in dict["animals"]:
      ... dict["animals"] = [dict["animals"], "dog"]





      share|improve this answer


















      • 3





        try to add a third animal that will nest your list once more.

        – Jean-François Fabre
        Nov 14 '18 at 15:03











      • that's not in the problem statement

        – Jonathan R
        Nov 14 '18 at 15:04











      • Thanks. Yeah it would be good to append more strings and get just a list of strings as @ Jean-François Fabre noticed

        – James Flash
        Nov 14 '18 at 15:06














      1












      1








      1







      >>> if "dog" not in dict["animals"]:
      ... dict["animals"] = [dict["animals"], "dog"]





      share|improve this answer













      >>> if "dog" not in dict["animals"]:
      ... dict["animals"] = [dict["animals"], "dog"]






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 14 '18 at 15:00









      Jonathan RJonathan R

      1,162512




      1,162512







      • 3





        try to add a third animal that will nest your list once more.

        – Jean-François Fabre
        Nov 14 '18 at 15:03











      • that's not in the problem statement

        – Jonathan R
        Nov 14 '18 at 15:04











      • Thanks. Yeah it would be good to append more strings and get just a list of strings as @ Jean-François Fabre noticed

        – James Flash
        Nov 14 '18 at 15:06













      • 3





        try to add a third animal that will nest your list once more.

        – Jean-François Fabre
        Nov 14 '18 at 15:03











      • that's not in the problem statement

        – Jonathan R
        Nov 14 '18 at 15:04











      • Thanks. Yeah it would be good to append more strings and get just a list of strings as @ Jean-François Fabre noticed

        – James Flash
        Nov 14 '18 at 15:06








      3




      3





      try to add a third animal that will nest your list once more.

      – Jean-François Fabre
      Nov 14 '18 at 15:03





      try to add a third animal that will nest your list once more.

      – Jean-François Fabre
      Nov 14 '18 at 15:03













      that's not in the problem statement

      – Jonathan R
      Nov 14 '18 at 15:04





      that's not in the problem statement

      – Jonathan R
      Nov 14 '18 at 15:04













      Thanks. Yeah it would be good to append more strings and get just a list of strings as @ Jean-François Fabre noticed

      – James Flash
      Nov 14 '18 at 15:06






      Thanks. Yeah it would be good to append more strings and get just a list of strings as @ Jean-François Fabre noticed

      – James Flash
      Nov 14 '18 at 15:06












      1














      Should be the easies way I think



      dict = "vehicles": "car", "animals": "cat"
      if "dog" not in dict["animals"]:
      dict["animals"].add("dog")





      share|improve this answer





























        1














        Should be the easies way I think



        dict = "vehicles": "car", "animals": "cat"
        if "dog" not in dict["animals"]:
        dict["animals"].add("dog")





        share|improve this answer



























          1












          1








          1







          Should be the easies way I think



          dict = "vehicles": "car", "animals": "cat"
          if "dog" not in dict["animals"]:
          dict["animals"].add("dog")





          share|improve this answer















          Should be the easies way I think



          dict = "vehicles": "car", "animals": "cat"
          if "dog" not in dict["animals"]:
          dict["animals"].add("dog")






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 15:16

























          answered Nov 14 '18 at 15:07









          user3744881user3744881

          514




          514





















              1














              You just need to update animals list by using append method.



              dict = "vehicles": "car", "animals": "cat"

              def appendItem(item):
              if item not in dict["animals"]:
              if not isinstance(dict["animals"], list):
              dict["animals"] = [dict["animals"]]
              dict["animals"].append(item)

              appendItem("dog")
              appendItem("dog")
              appendItem("rabbit")
              appendItem("cat")


              Output



              => 'vehicles': 'car', 'animals': ['cat', 'dog', 'rabbit']





              share|improve this answer





























                1














                You just need to update animals list by using append method.



                dict = "vehicles": "car", "animals": "cat"

                def appendItem(item):
                if item not in dict["animals"]:
                if not isinstance(dict["animals"], list):
                dict["animals"] = [dict["animals"]]
                dict["animals"].append(item)

                appendItem("dog")
                appendItem("dog")
                appendItem("rabbit")
                appendItem("cat")


                Output



                => 'vehicles': 'car', 'animals': ['cat', 'dog', 'rabbit']





                share|improve this answer



























                  1












                  1








                  1







                  You just need to update animals list by using append method.



                  dict = "vehicles": "car", "animals": "cat"

                  def appendItem(item):
                  if item not in dict["animals"]:
                  if not isinstance(dict["animals"], list):
                  dict["animals"] = [dict["animals"]]
                  dict["animals"].append(item)

                  appendItem("dog")
                  appendItem("dog")
                  appendItem("rabbit")
                  appendItem("cat")


                  Output



                  => 'vehicles': 'car', 'animals': ['cat', 'dog', 'rabbit']





                  share|improve this answer















                  You just need to update animals list by using append method.



                  dict = "vehicles": "car", "animals": "cat"

                  def appendItem(item):
                  if item not in dict["animals"]:
                  if not isinstance(dict["animals"], list):
                  dict["animals"] = [dict["animals"]]
                  dict["animals"].append(item)

                  appendItem("dog")
                  appendItem("dog")
                  appendItem("rabbit")
                  appendItem("cat")


                  Output



                  => 'vehicles': 'car', 'animals': ['cat', 'dog', 'rabbit']






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 14 '18 at 15:29

























                  answered Nov 14 '18 at 15:00









                  Mihai Alexandru-IonutMihai Alexandru-Ionut

                  30.5k64175




                  30.5k64175



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303080%2fconversion-of-single-string-to-list-of-strings-when-new-strings-are-added%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo