Python | Convert list of strings to list of tuples
Sometimes we deal with different types of data types and we require to inter convert from one data type to another and hence inter conversion always a useful tool to have knowledge about. The interconversion from tuples to other formats have been discussed earlier. This article deals with the converse case. Let’s discuss certain ways in which this can be done.
Method #1 : Using map() + split() + tuple()
This task can be achieved using the combination of these functions. The map function can be used to link the logic to each string, split function is used to split the inner contents of list to different tuple attributes and tuple function performs the task of forming a tuple.
# Python3 code to demonstrate # convert list of strings to list of tuples # Using map() + split() + tuple() # initializing list test_list = ['4, 1', '3, 2', '5, 3'] # printing original list print("The original list : " + str(test_list)) # using map() + split() + tuple() # convert list of strings to list of tuples res = [tuple(map(int, sub.split(', '))) for sub in test_list] # print result print("The list after conversion to tuple list : " + str(res)) |
The original list : ['4, 1', '3, 2', '5, 3'] The list after conversion to tuple list : [(4, 1), (3, 2), (5, 3)]
Method #2 : Using map() + eval
This is the most elegant way to perform this particular task. Where map function is used to extend the function logic to the whole list eval function internally performs interconversions and splitting.
# Python3 code to demonstrate # convert list of strings to list of tuples # Using map() + eval # initializing list test_list = ['4, 1', '3, 2', '5, 3'] # printing original list print("The original list : " + str(test_list)) # using map() + eval # convert list of strings to list of tuples res = list(map(eval, test_list)) # print result print("The list after conversion to tuple list : " + str(res)) |
The original list : ['4, 1', '3, 2', '5, 3'] The list after conversion to tuple list : [(4, 1), (3, 2), (5, 3)]
Recommended Posts:
- Python | Convert list of tuples to list of strings
- Python | Convert list of tuples to list of list
- Python | Convert list of tuples into list
- Python | Convert dictionary to list of tuples
- Python | Convert list of tuples into digits
- Python | Convert a list of Tuples into Dictionary
- Python | Convert list of tuples to dictionary value lists
- Python | Remove all strings from a list of tuples
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Find the tuples containing the given element from a list of tuples
- Python | Remove duplicate tuples from list of tuples
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Count tuples occurrence in list of tuples
- Python | Remove tuples from list of tuples if greater than n
- Python | Update a list of tuples using another list
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.



