{"id":2627,"date":"2020-01-17T18:35:53","date_gmt":"2020-01-17T18:35:53","guid":{"rendered":"https:\/\/www.askpython.com\/?p=2627"},"modified":"2021-06-21T07:31:12","modified_gmt":"2021-06-21T07:31:12","slug":"python-convert-string-to-list","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/string\/python-convert-string-to-list","title":{"rendered":"Python &#8211; Convert String to List"},"content":{"rendered":"\n<p>In Python, if you ever need to deal with codebases that perform various calls to other APIs, there may be situations where you may receive a string in a list-like format, but still not explicitly a list. In situations like these, you may want to convert the string into a list. <\/p>\n\n\n\n<p>In this article, we will look at some ways of achieving the same on Python.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Converting List-type strings<\/h2>\n\n\n\n<p>A <em>list-type<\/em> string can be a string that has the opening and closing parenthesis as of a list and has comma-separated characters for the list elements. The only difference between that and a list is the opening and closing quotes, which signify that it is a string.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstr_inp = &#039;&#x5B;&quot;Hello&quot;, &quot;from&quot;, &quot;AskPython&quot;]&#039;\n<\/pre><\/div>\n\n\n<p>Let us look at how we can convert these types of strings to a list.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Method 1: Using the ast module<\/h3>\n\n\n\n<p>Python&#8217;s <code>ast<\/code> (Abstract Syntax Tree) module is a handy tool that can be used to deal with strings like this, dealing with the contents of the given string accordingly.<\/p>\n\n\n\n<p>We can use <code>ast.literal_eval()<\/code> to evaluate the literal and convert it into a list.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport ast\n\nstr_inp = &#039;&#x5B;&quot;Hello&quot;, &quot;from&quot;, &quot;AskPython&quot;]&#039;\nprint(str_inp)\nop = ast.literal_eval(str_inp)\nprint(op)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#039;&#x5B;&quot;Hello&quot;, &quot;from&quot;, &quot;AskPython&quot;]&#039;\n&#x5B;&#039;Hello&#039;, &#039;from&#039;, &#039;AskPython&#039;]\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Method 2: Using the json module<\/h3>\n\n\n\n<p>Python&#8217;s <code>json<\/code> module also provides us with methods that can manipulate strings.<\/p>\n\n\n\n<p>In particular, the <code>json.loads()<\/code> method is used to decode JSON-type strings and returns a list, which we can then use accordingly.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport json\n\nstr_inp = &#039;&#x5B;&quot;Hello&quot;, &quot;from&quot;, &quot;AskPython&quot;]&#039;\nprint(str_inp)\nop = json.loads(str_inp)\nprint(op)\n<\/pre><\/div>\n\n\n<p>The output remains the same as before.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Method 3: Using str.replace() and str.split()<\/h3>\n\n\n\n<p>We can use Python&#8217;s in-built <code>str.replace()<\/code> method and manually iterate through the input string.<\/p>\n\n\n\n<p>We can remove the opening and closing parenthesis while adding elements to our newly formed list using <code>str.split(\",\")<\/code>, parsing the list-type string manually.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstr_inp = &#039;&#x5B;&quot;Hello&quot;, &quot;from&quot;, &quot;AskPython&quot;]&#039;\nstr1 = str_inp.replace(&#039;]&#039;,&#039;&#039;).replace(&#039;&#x5B;&#039;,&#039;&#039;)\nop = str1.replace(&#039;&quot;&#039;,&#039;&#039;).split(&quot;,&quot;)\nprint(op)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;&#039;Hello&#039;, &#039; from&#039;, &#039; AskPython&#039;]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Converting Comma separated Strings<\/h2>\n\n\n\n<p>A <em>comma-separated string is a string that<\/em> has a sequence of characters, separated by a comma, and enclosed in Python&#8217;s string quotations.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstr_inp = &quot;Hello,from,AskPython&#039;\n<\/pre><\/div>\n\n\n<p>To convert these types of strings to a list of elements, we have some other ways of performing the task.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Method 1: Using str.split(&#8216;,&#8217;)<\/h3>\n\n\n\n<p>We can directly convert it into a list by separating out the commas using <code>str.split(',')<\/code>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstr_inp = &quot;Hello,from,AskPython&quot;\nop = str_inp.split(&quot;,&quot;)\nprint(op)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;&#039;Hello&#039;, &#039;from&#039;, &#039;AskPython&#039;]\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Method 2: Using eval()<\/h3>\n\n\n\n<p>If the input string is trusted, we can spin up an interactive shell and directly evaluate the string using <code>eval()<\/code>.<\/p>\n\n\n\n<p>However, this is <strong>NOT<\/strong> recommended, and should rather be avoided, due to security hazards of running potentially untrusted code.<\/p>\n\n\n\n<p>Even so, if you still want to use this, go ahead. We warned you!<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstr_inp = &quot;potentially,untrusted,code&quot;\n\n# Convert to a quoted string so that\n# we can use eval() to convert it into\n# a normal string\nstr_inp = &quot;&#039;&quot; + str_inp + &quot;&#039;&quot;\nstr_eval = &#039;&#039;\n\n# Enclose every comma within single quotes\n# so that eval() can separate them\nfor i in str_inp:\n    if i == &#039;,&#039;:\n        i = &quot;&#039;,&#039;&quot;\n    str_eval += i\n\nop = eval(&#039;&#x5B;&#039; + str_eval + &#039;]&#039;)\nprint(op)\n<\/pre><\/div>\n\n\n<p>The output will be a list, since the string has been evaluated and a parenthesis has been inserted to now signify that it <code>op<\/code> is a list.<\/p>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;&#039;potentially&#039;, &#039;untrusted&#039;, &#039;code&#039;]\n<\/pre><\/div>\n\n\n<p>This is quite long and is not recommended for parsing out comma-separated strings. Using <code>str.split(',')<\/code> is the obvious choice in this case.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, we learned some ways of converting a list into a string. We dealt with list-type strings and comma-separated strings and converted them into Python lists.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/stackoverflow.com\/questions\/5387208\/how-to-convert-a-string-with-comma-delimited-items-to-a-list-in-python\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">StackOverflow post on converting String to List<\/a><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n","protected":false},"excerpt":{"rendered":"<p>In Python, if you ever need to deal with codebases that perform various calls to other APIs, there may be situations where you may receive a string in a list-like format, but still not explicitly a list. In situations like these, you may want to convert the string into a list. In this article, we [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-2627","post","type-post","status-publish","format-standard","hentry","category-string"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2627","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=2627"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2627\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=2627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=2627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=2627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}