There are a few useful tips to convert a Python list (or any other iterable such as a tuple) to a string for display. Show First, if it is a list of strings, you may simply use join this way: >>> mylist = ['spam', 'ham', 'eggs'] >>> print ', '.join(mylist) spam, ham, eggsUsing the same method, you might also do this: >>> print '\n'.join(mylist) spam ham eggsHowever, this simple method does not work if the list contains non-string objects, such as integers. If you just want to obtain a comma-separated string, you may use this shortcut: >>> list_of_ints = [80, 443, 8080, 8081] >>> print str(list_of_ints).strip('[]') 80, 443, 8080, 8081Or this one, if your objects contain square brackets: >>> print str(list_of_ints)[1:-1] 80, 443, 8080, 8081Finally, you may use map() to convert each item in the list to a string, and then join them: >>> print ', '.join(map(str, list_of_ints)) 80, 443, 8080, 8081 >>> print '\n'.join(map(str, list_of_ints)) 80 443 8080 8081While programming we may need to convert a string to 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 are going to learn how we can convert a string into a list in Python. Methods of converting a string to list in PythonConversion of a string to list in Python is a pretty easy job. It can be achieved by following different methods as per our own requirements. Here in this tutorial, we are going to deal with all the methods using which we can convert a string to list in Python for different cases. Below we have listed all the methods:
Now we are going to discuss each one of the above-mentioned techniques one-by-one. 1. String to List of StringsWhen we need to convert a string to list in Python containing the constituent strings of the parent string(previously separated by some separator like ‘,’ or space), we use this method to accomplish the task. For example, say we have a string “Python is great”, and we want a list which would contain only the given names previously separated by spaces, we can get the required list just by splitting the string into parts on the basis of the position of space. Let us look at an example to understand it better. #given string string1="Python is great" #printing the string print("Actual String: ",string1) #gives us the type of string1 print("Type of string: ",type(string1)) print("String coverted to list :",string1.split()) #prints the list given by split() Output: In the above code:
2. String to List of CharactersWhat if we need a list of characters present in a string? In that case, direct type conversion from string to list in Python using the list() method does the job for us. Certainly, if the input string is something like “abcd”, typecasting the string into a list using the list() method gives us a list having the individual characters ‘a’, ‘b’, ‘c’, ‘d’ as its elements. Take a look at the given example code below. #given string string1="AskPython" #printing the string print("Actual String: ",string1) #confirming the type() print("Type of string: ",type(string1)) #type-casting the string into list using list() print("String coverted to list :\n",list(string1)) Output: Understanding the code:
3. List of Strings to List of ListsHere, we are going to see how we can combine both the above methods to convert a string to a list of character lists. Look at the below-given example carefully, #Given string string1="This is Python" print("The actual string:",string1) #converting string1 into a list of strings string1=string1.split() #applying list method to the individual elements of the list string1 list1=list(map(list,string1)) #printing the resultant list of lists print("Converted to list of character list :\n",list1) Output: Understand the code:
4. CSV to ListA CSV( Comma Separated Values) string, as its name suggests is a string consisting of values or data separated by commas. Let us look at how we can convert such type of string to list in Python. #given string string1="abc,def,ghi" print("Actual CSV String: ",string1) print("Type of string: ",type(string1)) #spliting string1 into list with ',' as the parameter print("CSV coverted to list :",string1.split(',')) Output: Here:
5. A string consisting of Integers to List of integersNow we are going to convert a string consisting of only integers separated by some space, comma or etc., to a list with integer type elements. For example, look at the code below, #string with integers sepated by spaces string1="1 2 3 4 5 6 7 8" print("Actual String containing integers: ",string1) print("Type of string: ",type(string1)) #coverting the string into list of strings list1=list(string1.split()) print("Converted string to list : ",list1) #typecasting the individual elements of the string list into integer using the map() method list2=list(map(int,list1)) print("List of integers : ",list2) Output: Now:
ConclusionThat’s all now, this was all 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. References
|