Python | Convert String to list of tuples
Sometimes, while working with data, we can have a problem in which we have a string list of data and we need to convert the same to list of records. This kind of problem can come when we deal with a lot of string data. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using zip() + split() + list slicing
The combination of above functionalities can be used to solve this problem. In this, first, the string is converted to list of strings and then required tuples are made using zip()
and list slicing functionality.
# Python3 code to demonstrate working of # Convert String to list of tuples # Using zip() + list slicing + split() # initialize string test_string = "GFG is best Computer Science Portal" # printing original string print ( "The original string : " + str (test_string)) # Convert String to list of tuples # Using zip() + list slicing + split() temp = test_string.split() res = list ( zip (temp[:: 2 ], temp[ 1 :: 2 ])) # printing result print ( "List after String to tuple conversion : " + str (res)) |
The original string : GFG is best Computer Science Portal
List after String to tuple conversion : [(‘GFG’, ‘is’), (‘best’, ‘Computer’), (‘Science’, ‘Portal’)]
Method #2 : Using iter() + split() + next()
+ generator expression
This is yet another method to perform this particular task. In this, we use iterator to reach to solution of this task. Method is same as above, just iterator is used for faster access.
# Python3 code to demonstrate working of # Convert String to list of tuples # Using iter() + split() + next() + generator expression # initialize string test_string = "GFG is best Computer Science Portal" # printing original string print ( "The original string : " + str (test_string)) # Convert String to list of tuples # Using iter() + split() + next() + generator expression temp = iter (test_string.split()) res = [(ele, next (temp)) for ele in temp] # printing result print ( "List after String to tuple conversion : " + str (res)) |
The original string : GFG is best Computer Science Portal
List after String to tuple conversion : [(‘GFG’, ‘is’), (‘best’, ‘Computer’), (‘Science’, ‘Portal’)]
Recommended Posts:
- Python | Convert string tuples to list tuples
- Python | Convert list of strings to list of tuples
- Python | Convert list of tuples to list of strings
- Python | Convert dictionary to list of tuples
- Python | Convert list elements to bi-tuples
- Python | Convert a list of Tuples into Dictionary
- Python | Convert list of tuples into digits
- Python | Convert list of tuples to dictionary value lists
- Python | Convert list of tuples into list
- Python | Convert list of tuples to list of list
- Python | List of tuples to String
- Python | Flatten Tuples List to String
- Python | Count tuples occurrence in list of tuples
- Python | Remove tuples from list of tuples if greater than n
- Python | Remove tuples having duplicate first value from given list of tuples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.