Open In App

Convert list of strings to list of tuples in Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Sometimes we deal with different types of data types and we require to inter-convert from one data type to another hence interconversion is always a useful tool to have knowledge. This article deals with the converse case. Let’s discuss certain ways in which this can be done in Python.

Method 1: Convert a list of strings to a list of tuples using the implicit method

In this method, we simply use the tuple() method to convert the list into a tuple.

Python3




sample_list = ['Geeks', 'For', 'Geeks']
li = []
#convert list into tuple
tuple1 = tuple(sample_list)
li.append(tuple1)
print(li)
print(type(li))


Output:

[('Geeks', 'For', 'Geeks')]
<class 'list'>

Method 2: Convert a list of strings to a list of tuples using the dereferencing method

In this method, we are using the asterisk operator for dereferencing it to convert a list of strings to a list of tuples.

Python3




sample_list = ['Geeks', 'For', 'Geeks']
 
li = []
# unpack list items and form tuple
tuple1 = (*sample_list,)
 
tuple1 = li.append(tuple1)
 
print(li)
print(type(li))


Output:

[('Geeks', 'For', 'Geeks')]
<class 'list'>

Method 3: 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, the split function is used to split the inner contents of the list into different tuple attributes and the tuple function performs the task of forming a tuple. 

Python3




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


Output :

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

Method 4: Using map() + eval 

This is the most elegant way to perform this particular task. Where the map function is used to extend the function logic to the whole list eval function internally performs interconversions and splitting. 

Python3




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


Output:

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

Method 5: Using ast

Algorithm:

Import the ast module.
Initialize a list of strings that needs to be converted into tuples.
Print the original list.
Use ast.literal_eval() function to convert each string in the list into a tuple of integers.
Store each tuple in a new list called res.
Print the list after conversion to tuple list.

Python3




import ast
 
# initializing list
test_list = ['4, 1', '3, 2', '5, 3']
 
# printing original list
print("The original list : " + str(test_list))
 
# using ast.literal_eval()
# convert list of strings to list of tuples
res = [tuple(map(int, ast.literal_eval(s))) for s in test_list]
 
# print result
print("The list after conversion to tuple list : " + str(res))


Output

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

The time complexity of this code is O(n), where n is the number of strings in the input list. The space complexity is also O(n), since we create a new list of tuples with the same length as the input list.

Method 9: Using the __iadd__ method and a list containing the tuple.

Algorithm:

  1. Convert the input list of strings to a tuple using tuple(sample_list).
  2. Create an empty list li.
  3. Append the tuple to li using the __iadd__ method and a list containing the tuple.
  4. Print the original list and the converted list.

Python3




sample_list =  ['4, 1', '3, 2', '5, 3']
li = []
tuple1 = tuple(sample_list)
li.__iadd__([tuple1])
print("The original list : " + str(sample_list))
 
# print result
print("The list after conversion to tuple list : " + str(li))


Output

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

Time complexity:
The time complexity of this implementation depends on the length of the input list of strings. Converting a list to a tuple has a time complexity of O(n), where n is the length of the list. Appending a tuple to a list using the __iadd__ method has a time complexity of O(1). Therefore, the overall time complexity of this implementation is O(n).

Space complexity:
The space complexity of this implementation also depends on the length of the input list of strings. Converting a list to a tuple requires additional memory and has a space complexity of O(n), where n is the length of the list. Appending the tuple to the list li using the __iadd__ method does not create a new list and has a space complexity of O(1). Therefore, the overall space complexity of this implementation is O(n).



Last Updated : 02 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads