{"id":3559,"date":"2020-02-27T10:35:02","date_gmt":"2020-02-27T10:35:02","guid":{"rendered":"https:\/\/www.askpython.com\/?p=3559"},"modified":"2024-01-16T02:11:33","modified_gmt":"2024-01-16T02:11:33","slug":"convert-string-to-list-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/string\/convert-string-to-list-in-python","title":{"rendered":"Convert String to List in Python"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">While programming we may need to convert a <strong>string<\/strong> to a <strong>list<\/strong> in Python. That could be for any other reason. But, a question arises here, how can we convert a string to different forms of lists?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, here in this tutorial, we will learn how to convert a string into a list in <strong>Python<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Methods of converting a string to a list in Python<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Conversion of a <a class=\"rank-math-link\" href=\"https:\/\/www.askpython.com\/python\/string\">string<\/a> to a list in Python is a pretty easy job. It can be achieved by following different methods as per our requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here in this tutorial, we are going to deal with all the methods using which we can convert a string to a list in Python for different cases. Below we have listed all the methods:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>A String to List of Strings<\/strong><\/li>\n\n\n\n<li><strong>A String to List of Characters<\/strong><\/li>\n\n\n\n<li><strong>List of Strings to List of Lists<\/strong><\/li>\n\n\n\n<li><strong>CSV to List<\/strong><\/li>\n\n\n\n<li><strong>A string consisting of Integers to a List of integers<\/strong><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Now we are going to discuss each one of the above-mentioned techniques one by one.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. String to List of Strings<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When we need to convert a string to a list in Python containing the constituent strings of the parent string (previously separated by some separator like<strong> &#8216;,&#8217;<\/strong> or space), we use this method to accomplish the task.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, say we have a string <strong>&#8220;Python is great&#8221;<\/strong>, and we want a list that would contain only the given names previously separated by spaces, we can get the required list just by splitting the string into parts based on the position of space.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let us look at an example to understand it better.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#given string\nstring1=&quot;Python is great&quot;\n\n#printing the string\nprint(&quot;Actual String: &quot;,string1) \n  \n#gives us the type of string1\nprint(&quot;Type of string: &quot;,type(string1))  \n\nprint(&quot;String converted to list :&quot;,string1.split()) \n#prints the list given by split()\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong>:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"526\" height=\"285\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/02\/String-to-List-of-Strings.png\" alt=\"String To List in Python\" class=\"wp-image-3572\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/02\/String-to-List-of-Strings.png 526w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/02\/String-to-List-of-Strings-300x163.png 300w\" sizes=\"auto, (max-width: 526px) 100vw, 526px\" \/><figcaption class=\"wp-element-caption\">String To List Of Strings<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In the above code:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We consider a string, <code>string1=\"Python is great\"<\/code> and try to convert the same list of the constituent strings<\/li>\n\n\n\n<li>type() gives us the type of object passed to the method, which in our case was a string<\/li>\n\n\n\n<li>split() is used to split a string into a list based on the given separator. In our code, the words were separated by spaces. By default, if we do not pass anything to the split() method it splits up the string based on the position of spaces<\/li>\n\n\n\n<li>Hence though we have not mentioned the separator parameter, the <code>split()<\/code> method gives us a list of the respective strings<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. String to List of Characters<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">What if we need a <strong>list of characters<\/strong> present in a string? In that case, direct type conversion from string to list in Python using the <code>list()<\/code> method does the job for us.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Certainly, if the input string is something like<strong> &#8220;abcd&#8221;<\/strong>, typecasting the string into a list using the <code>list()<\/code> method gives us a list having the individual characters <strong>&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;, &#8216;d&#8217;<\/strong> as its elements. Take a look at the given example code below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#given string\nstring1=&quot;AskPython&quot;\n\n#printing the string\nprint(&quot;Actual String: &quot;,string1)\n#confirming the type()\nprint(&quot;Type of string: &quot;,type(string1))\n\n#type-casting the string into list using list()\nprint(&quot;String converted to list :\\n&quot;,list(string1))\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong>:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"527\" height=\"298\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/02\/string-to-list-of-characters.png\" alt=\"String To List Of Characters\" class=\"wp-image-3573\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/02\/string-to-list-of-characters.png 527w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/02\/string-to-list-of-characters-300x170.png 300w\" sizes=\"auto, (max-width: 527px) 100vw, 527px\" \/><figcaption class=\"wp-element-caption\">String To List Of Characters<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding the code:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Firstly here, we initialize a string, <code>string1<\/code> as <strong>&#8220;AskPython&#8221;<\/strong> and print its type using the <code>type()<\/code> method<\/li>\n\n\n\n<li>And as we can observe, typecasting the string using the <code>list()<\/code> method gives us a list of the member characters, as required<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. List of Strings to List of Lists<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here, we are going to see how we can combine both the above methods to <strong>convert a string to a list of character lists<\/strong>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Look at the below-given example carefully,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#Given string\nstring1=&quot;This is Python&quot;\n\nprint(&quot;The actual string:&quot;,string1)\n\n#converting string1 into a list of strings\nstring1=string1.split()\n\n#applying list method to the individual elements of the list string1\nlist1=list(map(list,string1))\n\n#printing the resultant list of lists\nprint(&quot;Converted to list of character list :\\n&quot;,list1)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong>:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"652\" height=\"282\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/02\/string-to-list-of-character-lists.png\" alt=\"String To List Of Character Lists\" class=\"wp-image-3574\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/02\/string-to-list-of-character-lists.png 652w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/02\/string-to-list-of-character-lists-300x130.png 300w\" sizes=\"auto, (max-width: 652px) 100vw, 652px\" \/><figcaption class=\"wp-element-caption\">String To List Of Character Lists<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Understand the code:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In this case, after the initialization of the string <code>string1<\/code>, we use the first method and convert it into a list of strings<\/li>\n\n\n\n<li>That is, at this point string1 is a list of strings given by <code>[ 'This', 'is', 'Python' ]<\/code><\/li>\n\n\n\n<li>Then we apply the <code>list()<\/code> method to all the elements of the list<\/li>\n\n\n\n<li><strong>string1<\/strong>. As we saw in our previous case this gives us a list consisting of character lists. Note, mass type-casting was performed using the <a href=\"https:\/\/www.askpython.com\/python\/built-in-methods\/map-method-in-python\" class=\"rank-math-link\">map() function<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. CSV to List<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A <strong>CSV( Comma Separated Values)<\/strong> string, as its name suggests is a string consisting of values or data separated by commas.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let us look at how we can convert the such type of string to a list in Python.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#given string\nstring1=&quot;abc,def,ghi&quot;\nprint(&quot;Actual CSV String: &quot;,string1)\nprint(&quot;Type of string: &quot;,type(string1))\n\n#spliting string1 into list with &#039;,&#039; as the parameter\nprint(&quot;CSV converted to list :&quot;,string1.split(&#039;,&#039;))\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong>:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"470\" height=\"293\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/02\/CSV-to-list.png\" alt=\"CSV To List\" class=\"wp-image-3571\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/02\/CSV-to-list.png 470w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/02\/CSV-to-list-300x187.png 300w\" sizes=\"auto, (max-width: 470px) 100vw, 470px\" \/><figcaption class=\"wp-element-caption\">CSV To List<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Here:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Similarly, we initiate by considering a string <strong>string1<\/strong> with various data or values separated by commas(<strong>&#8216;,&#8217;<\/strong>)<\/li>\n\n\n\n<li>After printing it and its <code>type()<\/code>, we proceed by splitting it based on the parameter &#8216;,&#8217;<\/li>\n\n\n\n<li>This makes the values &#8216;abc&#8217;, &#8216;def&#8217;, and &#8216;ghi&#8217; the elements of a list. In this way, we were able to extract values from a given <strong>CSV<\/strong><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. A string consisting of Integers to a List of integers<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now we are going to convert a string consisting of only integers separated by some space, comma, etc., to a list with <strong>integer<\/strong> elements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, look at the code below,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#string with integers sepated by spaces\nstring1=&quot;1 2 3 4 5 6 7 8&quot;\nprint(&quot;Actual String containing integers: &quot;,string1)\nprint(&quot;Type of string: &quot;,type(string1))\n\n#converting the string into list of strings\nlist1=list(string1.split())\nprint(&quot;Converted string to list : &quot;,list1)\n\n#typecasting the individual elements of the string list into integer using the map() method\nlist2=list(map(int,list1))\nprint(&quot;List of integers : &quot;,list2)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong>:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"664\" height=\"299\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/02\/string-with-integers-to-list.png\" alt=\"String With Integers To List\" class=\"wp-image-3570\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/02\/string-with-integers-to-list.png 664w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/02\/string-with-integers-to-list-300x135.png 300w\" sizes=\"auto, (max-width: 664px) 100vw, 664px\" \/><figcaption class=\"wp-element-caption\">String With Integers To List<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Now:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We took a string, <code>string1<\/code> as<strong> &#8220;1 2 3 4 5 6 7 8&#8221;<\/strong> and print it and its <strong>type()<\/strong> consecutively<\/li>\n\n\n\n<li>Then we split it using the <code>split()<\/code> method and store the resultant list into a list, <strong>list1<\/strong>. At this point, list1 holds <strong>[ &#8216;1&#8217;, &#8216;2&#8217; , &#8216;3&#8217;, &#8216;4&#8217;, &#8216;5&#8217;, &#8216;6&#8217;, &#8216;7&#8217;, &#8216;8&#8217; ]<\/strong> as we can see from the output, as expected<\/li>\n\n\n\n<li>Now we <a class=\"rank-math-link rank-math-link\" href=\"https:\/\/www.askpython.com\/python\/built-in-methods\/map-method-in-python\">map<\/a> the function <code>int()<\/code> throughout the list, typecasting each one of the elements into integers. And further, we store the typecasted mapped list into <strong>list2<\/strong> and print the same<\/li>\n\n\n\n<li>As a result, we get a list consisting of the integer elements on which now we can perform arithmetic operations.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">That&#8217;s all now, this was about converting strings into different lists using various methods. Try to use the one which suits your code and solves your purpose as well as meets up to your requirements. Questions in the comments are appreciated.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li> <a href=\"https:\/\/www.askpython.com\/python\/string\">https:\/\/www.askpython.com\/python\/string<\/a> <\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>While programming we may need to convert a string to a list in Python. That could be for any other reason. But, a question arises here, how can we convert a string to different forms of lists? So, here in this tutorial, we will learn how to convert a string into a list in Python. [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":3565,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-3559","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-string"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/3559","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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=3559"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/3559\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/3565"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=3559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=3559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=3559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}