How To Change Set To List In Python
Introduction
While programming we may need to convert a string to list in Python. That could exist for any other reason. But, a question arises here, how can we convert a string to unlike forms of lists?
So, here in this tutorial, we are going to learn how we can convert a cord into a list in Python.
Methods of converting a string to list in Python
Conversion of a string to list in Python is a pretty piece of cake task. Information technology can be achieved past post-obit unlike methods every bit per our own requirements.
Here in this tutorial, we are going to deal with all the methods using which we can catechumen a string to list in Python for unlike cases. Below we have listed all the methods:
- String to List of Strings
- String to List of Characters
- List of Strings to List of Lists
- CSV to List
- A string consisting of Integers to Listing of integers
Now we are going to discuss each one of the above-mentioned techniques one-past-one.
ane. String to List of Strings
When we need to convert a string to listing 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 keen", and nosotros want a list which would comprise only the given names previously separated by spaces, we tin can go the required listing just past splitting the string into parts on the ground of the position of infinite.
Allow us look at an example to understand it better.
#given string string1="Python is great" #printing the string print("Actual String: ",string1) #gives u.s.a. the type of string1 print("Type of string: ",type(string1)) print("String coverted to listing :",string1.split()) #prints the list given past split() Output:
In the higher up lawmaking:
- We consider a string,
string1="Python is great"and try to convert the same a listing of the constituent strings -
type()gives u.s. the blazon of object passed to the method, which in our case was a string -
divide()is basically used to split a string into a list on the basis of 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 on the basis of the position of spaces - Hence though we have not mentioned the separator parameter, the
split()method gives us a listing of the respective strings
2. String to List of Characters
What if we need a list of characters present in a string? In that case, direct blazon conversion from string to list in Python using the listing() method does the job for us.
Certainly, if the input cord is something like "abcd", typecasting the string into a list using the list() method gives the states a listing having the individual characters 'a', 'b', 'c', 'd' as its elements. Take a wait at the given example code below.
#given cord string1="AskPython" #press the string print("Bodily String: ",string1) #confirming the type() print("Type of string: ",type(string1)) #type-casting the cord into list using list() print("String coverted to listing :\north",list(string1)) Output:
Understanding the code:
- Firstly here, we initialize a string,
string1equally "AskPython" and print its blazon using thetype()method - And as we can discover, typecasting the cord using the
list()method gives the states a list of the fellow member characters, as required
3. List of Strings to Listing of Lists
Hither, we are going to meet how we can combine both the higher up methods to catechumen a cord to a listing of character lists.
Look at the below-given example carefully,
#Given string string1="This is Python" print("The actual cord:",string1) #converting string1 into a list of strings string1=string1.split() #applying list method to the individual elements of the listing string1 list1=list(map(listing,string1)) #printing the resultant list of lists impress("Converted to list of character list :\due north",list1) Output:
Empathise the code:
- In this case, afterwards the initialization of the string
string1, we use the first method and convert information technology into a list of strings - That is, at this betoken string1 is a listing of strings given past
[ 'This', 'is', 'Python' ] - Then nosotros utilise the
list()method to all the elements of the listing - string1. Equally we saw in our previous example this gives us a list consisting of grapheme lists. Annotation, mass blazon-casting was performed using the map() function
4. CSV to List
A CSV( Comma Separated Values) cord, as its name suggests is a string consisting of values or data separated past commas.
Let us expect at how nosotros can convert such type of cord 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 impress("CSV coverted to list :",string1.split(',')) Output:
Here:
- Similarly, we initiate by considering a string string1 with diverse information or values separated past commas(',')
- Afterwards press information technology and its
type(), we keep by splitting information technology on the basis of the parameter ',' - This makes the values 'abc', 'def', and 'ghi' the elements of a list. In this way, we were actually able to extract values from a given CSV
5. A cord consisting of Integers to List of integers
At present 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,
#cord with integers sepated by spaces string1="ane 2 three 4 5 6 7 8" print("Actual String containing integers: ",string1) print("Type of cord: ",type(string1)) #coverting the string into list of strings list1=listing(string1.split()) print("Converted string to listing : ",list1) #typecasting the individual elements of the cord list into integer using the map() method list2=listing(map(int,list1)) impress("List of integers : ",list2) Output:
At present:
- We took a string,
string1as "1 2 three 4 5 6 7 viii" and print information technology and its type() consecutively - And then we split information technology using the
split()method and store the resultant listing into a listing, list1. At this point, list1 holds [ 'i', '2' , '3', 'iv', '5', '6', '7', 'viii' ] as we can see from the output, every bit expected - Now we map the function
int()throughout the list, typecasting each one of the elements into integers. And further, nosotros store the typecasted mapped listing into list2 and print the same - As a effect, we go a list consisting of the integer elements on which now nosotros can perform arithmetic operations.
Conclusion
That's all at present, this was all nearly converting strings into different lists using various methods. Attempt to use the 1 which suits your lawmaking and solves your purpose as well every bit meets upwards to your requirements. Questions in the comments are appreciated.
References
- https://www.askpython.com/python/string
Source: https://www.askpython.com/python/string/convert-string-to-list-in-python

0 Response to "How To Change Set To List In Python"
Post a Comment