Python | Convert Integral list to tuple list
Last Updated :
12 Apr, 2023
Sometimes, while working with data, we can have a problem in which we need to perform type of interconversions of data. There can be a problem in which we may need to convert integral list elements to single element tuples. Let's discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension List comprehension can be used as a shorthand method to solve this particular problem. In this, we iterate through the list and convert each integer element into a tuple object.
Python3
# Python3 code to demonstrate working of
# Convert Integral list to tuple list
# using list comprehension
# initialize list
test_list = [1, 4, 6, 8, 9]
# printing original list
print("The original list : " + str(test_list))
# Convert Integral list to tuple list
# using list comprehension
res = [(ele, ) for ele in test_list]
# printing result
print("List after conversion to tuple list : " + str(res))
OutputThe original list : [1, 4, 6, 8, 9]
List after conversion to tuple list : [(1,), (4,), (6,), (8,), (9,)]
Time Complexity: O(n), where n is the length of the input list. This is because we’re using list comprehension which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method #2 : Using zip() + list() This is the simplest method to perform this particular task. The zip() when applied to list of integers, it converts each element to tuple. It returns a zip object and hence it has to converted back to list using list().
Python3
# Python3 code to demonstrate working of
# Convert Integral list to tuple list
# using zip() + list()
# initialize list
test_list = [1, 4, 6, 8, 9]
# printing original list
print("The original list : " + str(test_list))
# Convert Integral list to tuple list
# using zip() + list()
res = list(zip(test_list))
# printing result
print("List after conversion to tuple list : " + str(res))
OutputThe original list : [1, 4, 6, 8, 9]
List after conversion to tuple list : [(1,), (4,), (6,), (8,), (9,)]
Method #3 : Using map()
In this method, we use the map() function to iterate through the original list and convert each integer element into a tuple object using a lambda function. The map() returns an iterable map object which is then converted to a list using the list() function. This method is also simple and easy to use.
Python3
# Python3 code to demonstrate working of
# Convert Integral list to tuple list
# using map()
# initialize list
test_list = [1, 4, 6, 8, 9]
# printing original list
print("The original list : ", test_list)
# Convert Integral list to tuple list
# using map()
res = list(map(lambda x: (x,), test_list))
# printing result
print("List after conversion to tuple list : ", res)
OutputThe original list : [1, 4, 6, 8, 9]
List after conversion to tuple list : [(1,), (4,), (6,), (8,), (9,)]
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Python | Convert list to indexed tuple list Sometimes, while working with Python lists, we can have a problem in which we need to convert a list to tuple. This kind of problem have been dealt with before. But sometimes, we have it's variation in which we need to assign the index of the element along with element as a tuple. Let's discuss cert
3 min read
Convert List to Tuple in Python The task of converting a list to a tuple in Python involves transforming a mutable data structure list into an immutable one tuple. Using tuple()The most straightforward and efficient method to convert a list into a tuple is by using the built-in tuple(). This method directly takes any iterable like
2 min read
Python | Convert list of tuples into list In Python we often need to convert a list of tuples into a flat list, especially when we work with datasets or nested structures. In this article, we will explore various methods to Convert a list of tuples into a list. Using itertools.chain() itertools.chain() is the most efficient way to flatten a
3 min read
Python | Convert list of tuples to list of list Converting list of tuples to list of lists in Python is a task where each tuple is transformed into list while preserving its elements. This operation is commonly used when we need to modify or work with the data in list format instead of tuples.Using numpyNumPy makes it easy to convert a list of tu
3 min read
Convert List of Dictionary to Tuple list Python Given a list of dictionaries, write a Python code to convert the list of dictionaries into a list of tuples.Examples: Input: [{'a':[1, 2, 3], 'b':[4, 5, 6]}, {'c':[7, 8, 9], 'd':[10, 11, 12]}] Output: [('b', 4, 5, 6), ('a', 1, 2, 3), ('d', 10, 11, 12), ('c', 7, 8, 9)] Below are various methods to co
5 min read