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.



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

The original list : ['4, 1', '3, 2', '5, 3']
The list after conversion to tuple list : [(4, 1), (3, 2), (5, 3)]


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.