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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :



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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :

The original string : GFG is best Computer Science Portal
List after String to tuple conversion : [(‘GFG’, ‘is’), (‘best’, ‘Computer’), (‘Science’, ‘Portal’)]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.